Posted on March 16, 2023 by nexonhost
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:
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:
rm filename
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'?
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.
rm -f filename
If you want to get information about what is being removed, use the -v (verbose) option:
rm -v filename
removed 'filename'
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:
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:
rm *.png
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 dirname
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:
rm -r dirname
Prompt Before Removal
The -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 Enter:
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 -I option:
rm -i filename1 filename2 filename3 filename4
You will be asked to confirm the removal of all given files and directories:
rm: remove 4 arguments?
rm -rf
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:
rm -rf dirname
The rm -rf command is very dangerous and should be used with extreme caution!
Conclusion
We have shown you how to use the Linux rm command to remove files and directories from your Linux system.