Deleting Files and Directories with rm

The rm command has only one parameter of interest: --preserve-root. By now, you should know that issuing rm -rf / with sudo will destroy your Linux installation because -r means recursive and -f means force (do not prompt for confirmation before deleting). It is possible for a clumsy person to issue this command by accidentnot by typing the command on purpose, but by putting a space in the wrong place. For example:

rm -rf /home/paul

That command deletes the home directory of the user paul. This is not an uncommon command; after you have removed a user and backed up her data, you will probably want to issue something similar. However, if you add an accidental space between the / and the h in "home", you get this:

This time the command means "delete everything recursively from / and then delete home/paul"quite a different result! You can stop this from happening by using the --preserve-root parameter, which stops you from catastrophe with this message:

rm: it is dangerous to operate recursively on V' rm: use —no-preserve-root to override this failsafe.

Of course, no one wants to keep typing --preserve-root each time they run rm, so you should add this line to the .bashrc file in your home directory:

alias rm='rm —preserve-root'

That alias automatically adds --preserve-root to all calls to rm in future Bash sessions.

Was this article helpful?

0 0

Post a comment