The rm Command

The rm (i.e., remove) command is used to delete files and directories on Linux and other Unix-like operating systems.

The general syntax for rm is:

rm [options] [-r directories] filenames

The items in square brackets are optional. When used just with the names of one or more files, rm deletes all those files without requiring confirmation by the user. Thus, in the following example, rm would immediately delete the files named file1, file2 and file3, assuming that all three are located in the current directory (i.e., the directory in which the user is currently working):

rm file1 file2 file3

Error messages are returned if a file does not exist or if the user does not have the appropriate permission to delete it. Write-protected files prompt the user for a confirmation (with a y for yes and an n for no) before removal. Files located in write-protected directories can never be removed, even if those files are not write-protected.

The -f (i.e., force) option tells rm to remove all specified files, whether write-protected or not, without prompting the user. It does not display an error message or return error status if a specified file does not exist. However, if an attempt is made to remove files in a write-protected directory, this option will not suppress an error message.

The -i (i.e., interactive) option tells rm to prompt the user for confirmation before removing each file and directory. If both the -f and -i options are specified, the last one specified takes affect.

As a safety measure, rm does not delete directories by default. In order to delete directories, it is necessary to use the -r option, which is the same as the -R option. This option recursively removes directories and their contents in the argument list; that is, the specified directories will first be emptied of any subdirectories (including their subdirectories and files, etc.) and files and then removed. The user is normally prompted for removal of any write-protected files in the directories unless the -f option is used.

If a file encountered by rm is a symbolic link, the link is removed, but the file or directory to which that link refers will not be affected. A user does not need write permission to delete a symbolic link, as long as the user has write permission for the directory in which that link resides.

The rm command supports the -- (two consecutive dashes) parameter as a delimiter that indicates the end of the options. This is useful when the name of a file or directory begins with a dash or hyphen. For example, the following removes a directory named -dir1:

rm -r -- -dir1

Other options include -v (i.e., verbose), which provides additional information about what is happening, --help, which provides basic documentation about rm, and --version, which tells the version of rm that is currently in use. Some differences exist among the various versions of rm, so it is always wise to read the documentation for the particular system.

The rmdir command differs from rm in that it is only used to remove empty directories.

Created January 11, 2006.
Copyright © 2006 The Linux Information Project. All Rights Reserved.