How To Use Rm Command.
In this guide, we will explain how to use the rm command through examples and explanations of the most common rm options.
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.
How to Use the rm Command
The general syntax for the rm (remove) command is as follows:
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:
Type y and hit Enter to remove the file.
The -f option tells rm never to prompt the user and to ignore nonexistent files and arguments.
If you want to get information about what is being removed, use the -v (verbose) option:
Removing Multiple Files
Unlike the unlink command, rm allows you to delete multiple files at once. To do that, pass the filenames as arguments separated by space:
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 -d option:
rm -d is functionally identical to the rmdir command.
To remove non-empty directories and all the files within them recursively, use the
-r (recursive) option:
Prompt Before Removal
The -i option tells rm to prompt the user for each given file before removing it:
To confirm type y and press Enter:
When removing more than three files or recursively removing a directory, to get a single prompt for the entire operation, use the -I option:
You will be asked to confirm the removal of all given files and directories:
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 -f option:
The 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.