Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alias "rm -rf /"="echo 'idiot'" why not work?

Tags:

alias

shell

I wondered what if the name of an "alias" includes a command with arguments? I tried:

alias "rm -rf /"="echo 'idiot'"

but it does not work; what would be the correct way?

like image 299
user3125813 Avatar asked Apr 25 '26 21:04

user3125813


2 Answers

You can do it this way:

rm() {
  if [ "$1" = "-rf" ] && [ "$2" = "/" ]; then
    echo 'idiot'
  else
    command rm "$@"
  fi
}

Of course it doesn't match all cases, but you can modify this script if you want.

like image 193
Hauleth Avatar answered Apr 28 '26 12:04

Hauleth


There's no really good way to do that, though there have been attempts to do so.

The name of an alias or function must be a single identifier. rm -rf / is parsed as a command rm with two arguments, -rf and /. By the time that's done, it's too late for the shell to distinguish it from any other rm command.

On some systems, the default user startup scripts are configured to alias rm as rm -i, and of course you can do that yourself -- but since the -f option overrides -i, that doesn't help in this case.

The tcsh shell has a configuration shell variable $rmstar that causes it to recognize an rm command with the * wildcard as an argument; if it's set, the shell prompts the user for confirmation before executing the command. It's useful when you accidentally type rm * .o rather than rm *.o. But it doesn't catch rm -rf / because there's no wildcard. (And it doesn't help if you're not using tcsh.) Note that this can only be done in the shell itself; the rm command, or any replacement for it, doesn't see the * wildcard, it only sees a list of file names.

You can replace the rm command with something that prevents you, or tries to prevent you, from doing dangerous things. But any rm command is potentially dangerous. You could write an rm wrapper script that always prompts for confirmation before doing anything, and put it in, say, $HOME/bin -- but then you'll quickly develop the habit of dismissing the prompt any time you want to remove something.

You can replace the rm command with something that moves the specified files and/or directories into a "trash can" of some sort, from which you can easily recover them. But I often need to run rm -rf some-directory just to recover disk space. And again, this doesn't help for rm -rf /; that would delete the trash can directory as well, and even moving system files can ruin your day.

Solutions that look for specific command-line options are limited. Checking specifically for rm -rf / won't catch, rm -fr /, or rm --force --recursive /, or rm -rf /etc, which is nearly as dangerous as rm -rf /.

And any tweak that modifies the behavior of the rm command applies only when that tweak is available. If you manage to make the rm command safer in your own environment, you'll come to depend on it, which is likely to make you less careful when you're in another environment (say, on a machine where you haven't fully set up your account, or when you're using a different account for some reason).

The solution is to cultivate good habits.

  • Back up your data, and make sure you're actually able to recover files from your backups. (I'm not very good about this myself.)
  • Use the root account as rarely as possible. A non-root user running rm -rf / will do far less damage than root doing so. (I'm not sure whether a non-root user running rm -rf / will do anything; I'm not going to test it to find out.) Use sudo for individual commands rather than running a shell as root.
  • Think of rm as a dangerous command. If you've typed an rm command, re-read what you've typed before typing Enter.
like image 25
Keith Thompson Avatar answered Apr 28 '26 11:04

Keith Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!