The Linux rm command is one of the few that doesn’t actually pre-date UNIX. Pre-UNIX the command to remove files and directories was delete. It wasn’t until around 1971 that the rm command surfaced. Early versions of the command did not have any options to pass to it (such as -i or -rf). It wasn’t until 1973 that the command began to take the shape it has today. The rm command early on became a part of the Linux coreutils package (as it is to this date.)
In order to use the rm command you first have to have a terminal window open (such as aterm, gnome-terminal, or konsole). Once that terminal is open you can begin using rm at the Bash prompt. The rm command does follow UNIX permissions. In other words, if you do not own a file or directory (or do not have proper permissions to delete a file or directory) you cannot delete said file or directory. Say, for example, you want to remove the file test1 in the directory /home/maryjane. If you are already in the /home/maryjane directory you can just issue the command rm test1. But say you are in the /opt directory and you want to remove that same file without having to change to the /home/maryjane directory. To do this you just have to enter the full path to the file like so: rm /home/maryjane/test1 and the file will be removed.
Now what if you want to delete a directory? Even an empty directory (we’ll use the directory /home/maryjane/SAMPLE) will give you an error if you just enter rm /home/maryjane/SAMPLE. The error will read: rm: cannot remove `SAMPLE/’: Is a directory. In order to get rid of the directory you have to use the -r switch to recursively delete the contents of the directory and then the directory itself. Even if the directory is empty you have to use this switch. So now the command will look like this: rm -r /home/maryjane/SAMPLE.
Not So Basic Usage
The rm command is a very powerful command and with it you can delete anything and everything. The old Linux joke is to try to get someone to issue the command rm -rf / as the root user. By issue this command you will delete everything on your system. So DO NOT issue that command (you’ve been warned). Along these same lines is the ability to make the rm command interactive. By making the rm command interactive you can help yourself avoid deleting the wrong file or directory because you will have to answer (n)o or (y)es in order to delete anything. To make the rm command interactive you add the -i switch like so: rm -i /home/maryjane/test1. When the rm command is interactive you will see something like this:
[jlwallen@localhost ~]$ rm -i test1
rm: remove regular empty file `test1?’
To go ahead with the delete you press “y.” To cancel the delete you press “n.”
The rm command is another one of those tools that will quickly become invaluable to you. Learn it well, but always remember to NOT issue the command rm -rf / unless you want to totally delete a system. Again, you have been warned.
This post is part of the series: Keeping Your Linux Machine Clean
If you have used a PC for any length of time you know that a hard drive can very quickly become cluttered with files, remains of uninstalled applications, and disorganized files and directories. In this Brighthub series you will learn the tools to keep your Linux system clean.