self deleting bash script

How to create a bash script that deletes itself

This script will delete itself via the shred command (secure deletion) when it exits

#!/bin/bash
#
# Author: Steve Stonebraker
# Date: August 20, 2013
# Name: shred_self.sh
# Purpose: securely self-deleting shell script
# http://brakertech.com/self-deleting-bash-script

currentscript=$0

# function that is called when the script exits
function finish {
    echo "shredding ${currentscript}"; shred -u ${currentscript};
}

#whenver the script exits call the function "finish"
trap finish EXIT

Raw script can be found at: https://raw.github.com/ssstonebraker/braker-scripts/master/working-scripts/shred_self.sh #last line of script echo “exiting script”

Bash script that deletes itself and current directory if empty

#!/bin/bash
#
# Author: Steve Stonebraker
# Date: August 20, 2013
# Name: shred_self_and_dir.sh
# Purpose: securely self-deleting shell script, delete current directory if empty
# http://brakertech.com/self-deleting-bash-script

#set some variables
currentscript=$0
currentdir=$PWD

#export variable for use in subshell
export currentdir

# function that is called when the script exits
function finish {
    #securely shred running script
    echo "shredding ${currentscript}"
    shred -u ${currentscript};

    #if current directory is empty, remove it    
    if [ "$(ls -A ${currentdir})" ]; then
       echo "${currentdir} is not empty!"
    else
        echo "${currentdir} is empty, removing!"
        rmdir ${currentdir};
    fi

}

#whenver the script exits call the function "finish"
trap finish EXIT

#last line of script
echo "exiting script"

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>