AlfAlfa Posted December 31, 2015 Share Posted December 31, 2015 (edited) 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) 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. 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 January 2, 2016 by AlfAlfa Quote Link to comment Share on other sites More sharing options...
cooper Posted January 1, 2016 Share Posted January 1, 2016 alias rm "rm -i" Add to your ~/.bashrc to make permanent. Quote Link to comment Share on other sites More sharing options...
AlfAlfa Posted January 1, 2016 Author Share Posted January 1, 2016 (edited) 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 January 1, 2016 by AlfAlfa Quote Link to comment Share on other sites More sharing options...
cooper Posted January 2, 2016 Share Posted January 2, 2016 I believe -f overrides -i. Quote Link to comment Share on other sites More sharing options...
AlfAlfa Posted January 2, 2016 Author Share Posted January 2, 2016 Fair enough! I've added that information to my original post. Looks like Cooper's done it again folks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.