Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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

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/'

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/'

The script could start like this:


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


$ rm a * b
share|improve this answer
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
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… and… . Now it should work with code in my answer. – fedorqui Feb 9 '13 at 16:50

As I suggested in a different question, using a DEBUG trap that checks BASH_COMMAND will do the trick:

check_for_wildcard_rm() {
  local rm_glob_re='^rm[[:space:]](.*[[:space:]])?[*]([[:space:]]|$)'
  if [[ $BASH_COMMAND =~ $rm_glob_re ]]; then
    echo "Suppressing rm with a bare *" >&2
    return 1
  return 0
shopt -s extdebug
trap check_for_wildcard_rm DEBUG
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .