rm (remove files or directories)

This command is straightforward: it deletes a file. If you need to remove a whole directory, use the -r option (“recursive”):

rm -r dir

When rm hits a write-protected file, it will ask for your permission to delete it. This can be a nuisance with a directory containing many write-protected files. In this case, you may want to specify the -f option, which “forces” rm to remove any files it encounters without asking for permission. It goes without saying that this is a dangerous option. It can be very difficult or even impossible to recover files that you have accidentally deleted with rm.

I mostly use rm with the -i option (“interactive”), which makes rm ask for permission even when deleting files that are not write-protected. However, for obvious reasons, this is not feasible when deleting directories containing a large number of files. In such cases, I resort to rm -rf, but I double-check my command line before I press Enter.

Here is an even more important warning: you should never use the shell's * wildcard with rm. Let me explain why. Consider the following example:

rm *.txt

This deletes all files in the current directory ending in .txt. That's much quicker than typing rm file1.txt file2.txt, etc., but it's dangerous as hell. Imagine yourself inserting a space by mistake:

rm * .txt

If you now press Enter, rm will see two arguments on the command line, * and .txt. Consequently it will delete all files in the current directory (*) and the file .txt (which probably doesn't exist). This is an easy way to delete all files in your home directory by mistake.

Due to the safety concerns mentioned above, many people do not use rm at all. I have written a shell script called trash that I use instead. The script “trashes” files by moving them to a trash folder. I empty my trash folder once every few months using rm -rf. That's much safer than using rm on a daily basis. If you want to get into shell scripting, it would be a good and useful exercise to write your own trash script.