Jump to content

Life Saving Script #2 ~ Safe Remove (saferm)


AlfAlfa

Recommended Posts

I found this post here: https://forums.hak5.org/index.php?/topic/32757-life-saving-script/

Then today I was thinking about it, and thought of a way to possibly prevent having to use a script like that.

So I wrote this simple bash script which you can use instead of rm and it will force you to confirm whether you really want to execute that command or not:

Or you can skip the prompt after typing it correctly by having "-y" as the last argument!

#!/bin/bash

#AlfAlfa ~ saferm (safe remove, another life saving script! {shortened thanks to Cooper})
array=($@)
len=${#array[@]}
lastarg=${array[$len-1]}
args=${array[@]:0:$len-1}

if [[ $lastarg == '-y' ]]; then
rm $args
else
rm -i $args $lastarg
fi
exit

Since typos and accidentally hitting enter when you didn't mean to can happen, I think this is a good safeguard to make sure you really typed the command you wanted before actually going through with the removal!

Make it executable and copy it to /usr/bin or a similar directory so that you can execute it from anywhere.

Then use it like:

saferm /file/to/remove.txt

(Upon hitting enter it will ask you to confirm and you have to hit y to confirm)

Or like:

saferm -R /directory/and/files/to/remove -y

(the '-y' at the end like with apt-get will confirm without the prompt, and you're unlikely to type that after a bad command by accident)

cN6rdWO.png

EDIT: Thanks to Cooper it is now a bit shorter and better and also theres an easier way! Since rm -i already does the prompt, and better because it gives you more information about the operation that's about to take place, and prompts for individual files that it's about to remove. :smile:

Alternative method:

alias saferm "rm -i"

or

alias saferm "rm -I"

Add that to your ~/.bashrc or ~/.bash_aliases with whatever you want to call the alias even 'rm' to super impose that over rm...

Then when you use it, add an "-f" at the end of the line to skip the prompt(s) like "-y" from my version.

Edited by AlfAlfa
Link to comment
Share on other sites

alias rm "rm -i"
Add to your ~/.bashrc to make permanent.

Thanks Cooper! I feel a little bit silly now seeing that rm -i already does this. However not entirely, because it doesn't then have a way to skip the prompt(s) at the last argument when you know it's what you want.(not without back arrowing and backspacing the -i) The idea is that once you get to the end of typing your line, you know you haven't accidentally hit enter at that point and can just add the -y to still just do it without anything more of you. In the script '-y' has to be the last argument! (or it'll be passed to rm and that isn't a valid switch for rm) I didn't want the confirmation prompts always since I thought that would be a little annoying and I'd end up just using rm...

All in all this is still a little bit better than the setting an alias and adding it to ~/.bashrc! Thanks though as I've made my script a bit shorter now.

Edited by AlfAlfa
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...