The rm command is used to remove (delete) files and directories in Linux. rm (remove) deletes a file permanently, and can also be used to delete a directory, recursively deleting all the subdirectories. Unlike sending files to the Trashcan or Recycle Bin in a GUI interface, files deleted with rm cannot be recovered.
The basic syntax of rm command is as follows:
# rm [options] file-list
rm command options
This command has the following options:
There are several options that can be used with rm commands. Some of the most commonly used options are listed below:
|–interactive -i||Provides an interactive prompt before removing file.|
|–recursive -r||Deletes the contents of the specified directory, including all its subdirectories, and the directory itself|
|–verbose -v||Displays the name of each file as it is removed.|
|–force -f||Without asking for your consent, removes files for which you do not have write access permission. This option also suppresses informative messages if a file does not exist.|
Removing a file
The basic rm command deletes a file. For example:
# rm file01
After issuing the rm command for the files you want to delete, you might be presented with a response such as:
remove file01 (y/n)?
Removing a directory with rm
rm command can also be used to remove directories. Unlike rmdir which can only delete empty directories, rm along with “-r” option can delete non-empty directories and all subdirectories under it. For example:
# rm -fr /tmp/dir01
Removing files interactively
Use the -i flag to remove items interactively. Each individual operation is prompted for confirmation if you are removing files recursively with “-r” option. For example:
# rm -ri /tmp/dir01 examine files in directory /tmp/dir01? y remove /tmp/dir01/file02? y remove /tmp/dir01/file01? y remove /tmp/dir01? y
Remove files verbosely
Use the -v or –verbose option to make rm report each file or directory that’s removed:
# rm -rv /tmp/dir01 rm: descend into directory ‘/tmp/dir01’? y rm: remove regular empty file ‘/tmp/dir01/file01’? y removed ‘/tmp/dir01/file01’ rm: remove regular empty file ‘/tmp/dir01/file02’? y removed ‘/tmp/dir01/file02’ rm: remove directory ‘/tmp/dir01’? y removed directory: ‘/tmp/dir01’
Removing files silently for file without write access
The “-f” option allows you to remove the files for which you do not have the write access. It ignore nonexistent files and do not prompt. This overrides the –interactive (-i) option as well. Let’s see an example:
# rm -fr /tmp/dir01
Removing file with special characters in their filename
Its a bad practice to create filenames with special characters, but it might happen accidentally. When you want to delete these files, the “-i” option can come in handy as it confirms with the user before deleting any file. For example:
# rm -iv file* rm: remove regular empty file ‘file name with space’? y removed ‘file name with space’
To delete a file, you must have execute and write access permission to the parent directory of the file, but you do not need read or write access permission to the file itself. If you are running rm interactively (that is, if rm’s standard input is coming from the keyboard) and you do not have write access permission to the file, rm displays your access permission and waits for you to respond. If your response starts with a y or Y, rm deletes the file; otherwise, it takes no action.
You can not undo an rm command. So make sure you have a backup of the important file or use the interactive way of deleting files. Be particularly careful with wildcards. Whenever you use wildcards with rm, test the wildcard first with ls. This will let you see the files that will be deleted. Then press the up arrow key to recall the command and replace the ls with rm.