Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I wish to insure myself from accidentally using

rm [argument_i] * [argument_j]

i.e. it should always refuse to execute the command, when the asterisk character is separated by white space on both sides, no matter what else is before or after the asterisk.

Which command should I add to my .bashrc to enable such a check?

Thanks for any hints.

share|improve this question

migrated from stackoverflow.com Feb 15 '13 at 10:11

This question came from our site for professional and enthusiast programmers.

    
Related from ServerFault –  user000001 Feb 9 '13 at 14:37

1 Answer 1

I think you'd better define an alias to rm, so whenever you execute rm, it won't be rm itself but your alias.

In this alias you can parse everything.

alias rm='/my/path/myrm.sh'

And this script can have all the parameters parsing that you want.

This alias should be put in .bashrc / .bash_profile, or even in /etc/profile if you want it to be applying to all user.

Edit Following comments below, I added a wrapper that disables * interpreted as a list of all the files, and becomes just a string.

alias rm='set -f; /bin/sh /home/me/test/rm.sh'

The script could start like this:

#!/bin/sh

if echo "$2" | grep '*' > /dev/null; then
    echo "asterisk!"
else
    echo "hi this is $var1 and $var2 and $var3"
fi 

Test:

$ rm a * b
asterisk!
share|improve this answer
4  
I don't think relying on an alias for rm is a good idea. You'll get caught without the alias sometime, but you'll still be relying on its protection, and all hell will break loose. It is better to learn to think "rm is dangerous; let me check my command line" than to rely on a crutch. –  Jonathan Leffler Feb 9 '13 at 14:40
    
I agree with that, rm is quite dangerous and all of us have bad experiences with it :) Anyway, having an alias is the best solution I find for this case. Otherwise, you can also compile another rm but can be tough work. –  fedorqui Feb 9 '13 at 14:46
4  
I don't think this would help, as * is expanded by the shell prior to being sent as a parameter to your script or binary. So you won't find a * in the argument list of your script, but rather a list of all the files in the current directory. –  Costi Ciudatu Feb 9 '13 at 15:01
    
But you could write a wrapper script that checks if all files of the current directory are passed in the arguments and warns/aborts, if they are there –  BeniBela Feb 9 '13 at 15:54
    
Good to read all ideas. I took @BeniBela idea and got a couple of hints from stackoverflow.com/questions/1193658/… and stackoverflow.com/questions/3701809/… . Now it should work with code in my answer. –  fedorqui Feb 9 '13 at 16:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.