Bash alias-function that accepts a parameter: “srf” for “sudo rm -rf”

Add the following to your .bashrc file, and “reload” it with either of the following:

  • source ~/.bashrc
  • . ~/.bashrc
:<<COMMENT
   Alias-like function for recursive deletion, with are-you-sure prompt.
   Example call:
      srf /home/jeffy/django_files/rest_tutorial/rest_venv/

   With the following setting, this is *not* added to the history:
       export HISTIGNORE="*rm -r*:srf *"
   - http://superuser.com/questions/232885/can-you-share-wisdom-on-using-histignore-in-bash

   See:
   - Y/N prompt: http://stackoverflow.com/a/3232082/2736496
   - Alias w/param: http://stackoverflow.com/a/7131683/2736496
COMMENT
srf()  {
    #Actual line-breaks required in order to expand the variable.
    #- http://stackoverflow.com/a/4296147/2736496
    read -r -p "About to
sudo rm -rf $1
Are you sure? [y/N] " response
    response=${response,,}    # tolower
    if [[ $response =~ ^(yes|y)$ ]]
    then
        echo -e "sudo rm -rf $1"
        sudo rm -rf "$1";
    else
        echo "Cancelled."
    fi
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s