rm is a command-line utility for removing files and directories. It is one of the essential commands that every Linux user should be familiar with.
In this guide, we will explain how to use the
rm command through examples and explanations of the most common
How to Use the
The general syntax for the
rm (remove) command is as follows:
rm [OPTIONS]... FILE...
By default, when executed without any option,
rm doesn’t remove directories and doesn’t prompt the user for whether to proceed with the removal of the given files.
To delete a single file, use the
rm command followed by the file name as an argument:
If you don’t have write permissions on the parent directory, you will get “Operation not permitted” error.
If the file is not write protected, it will be removed without notice. On success, the command doesn’t produce any output and returns zero.
When removing write protected files, the command will prompt you for confirmation, as shown below:
rm: remove write-protected regular empty file 'filename'?
y and hit
Enter to remove the file.
-f option tells
rm never to prompt the user and to ignore nonexistent files and arguments.
rm -f filename
If you want to get information about what is being removed, use the
-v (verbose) option:
rm -v filename
Removing Multiple Files
rm allows you to delete multiple files at once. To do that, pass the filenames as arguments separated by space:
rm filename1 filename2 filename3
You can use regular expressions to match multiple files. For example, to remove all
.png files in the current directory, you would type:
When using regular expressions, before running the
rm command. is always a good idea to list the files with the
ls command so that you can see which files will be deleted.
Removing Directories (Folders)
To remove one or more empty directories use the
rm -d dirname
rm -d is functionally identical to the
To remove non-empty directories and all the files within them recursively, use the
-r (recursive) option:
rm -r dirname
Prompt Before Removal
-i option tells
rm to prompt the user for each given file before removing it:
rm -i filename1 filename2
To confirm type
y and press
rm: remove regular empty file 'filename1'? rm: remove regular empty file 'filename2'?
When removing more than three files or recursively removing a directory, to get a single prompt for the entire operation, use the
rm -i filename1 filename2 filename3 filename4
You will be asked to confirm the removal of all given files and directories:
rm: remove 4 arguments?
If the given directory or a file within the directory is write-protected, the
rm command will prompt you to confirm the operation. To remove a directory without being prompted, use the
rm -rf dirname
rm -rf command is very dangerous and should be used with extreme caution!
We have shown you how to use the Linux
rm command to remove files and directories from your Linux system.
Be extremely careful when removing important files or directories, because once the file is deleted, it cannot be easily recovered.