rm a command-line utility for removing files and directories. It is one of the essential commands that every Linux user should be familiar with. Whether you are removing a few files or deleting entire directories, "rm" is a reliable and efficient tool that can save you time and effort.
This article explains, 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 run without any options,
rm does not remove directories and does not prompt the user to remove the specified files.
To delete a single file, use the
rm command followed by the file name as an argument:
If you do not have write permissions on the root directory, you will receive an "Operation not permitted" error.
If the file is not write protected, it will be removed without notice. If successful, the command produces no output and returns zero.
When removing write-protected files, the command will ask for confirmation, as shown below:
rm: remove write-protected regular empty file 'filename'?
y and press
Enter to remove the file.
-f option tells
rm to never prompt the user and to ignore non-existent files and arguments.
rm -f filename
If you want to see the files that are removed, use the
-v option (verbose):
rm -v filename
Removing multiple files
rm allows you to delete multiple files at once. To do this, pass the file names to the command as space-separated arguments:
rm filename1 filename2 filename3
You can use regular expressions to match multiple files. For example, to remove all
.png files in the current directory, type:
When using regular expressions, before running the
rm command, it is always a good idea to list the files with the
ls command so 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
This option allows you to remove one or more empty directories without having to manually check them to make sure they are empty.
To recursively remove non-empty directories and all files within them, use the
-r (recursive) option:
rm -r dirname
The above command will delete the specified directory, including any files, directories, or symbolic links within it.
Request before removal
-i option tells
rm to prompt the user for each 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 to remove all specified files and directories:
rm: remove 4 arguments?
The rm command will ask you to confirm the operation if the specified directory or a file within the directory is write protected. To remove a directory without being prompted, use the
rm -rf dirname
Please note that the
rm -rf command is very dangerous and should be used with extreme caution and caution.
We have shown you how to use the
rm command on Linux to remove files and directories from your Linux system.
Be very careful when removing essential files or directories because once the file is deleted, it cannot be easily recovered.