rm command is the ultimate tool used to remove/delete files and directories in Linux distributions. The rm command is provided by all Linux distributions like Ubuntu, Debian, Mint, Kali, CentOS, RHEL, Fedora. There are alternatives to the rm command like rmdir but it provides only removal of the directories.
rm Command Syntax
rm command has the following syntax where is accept options and files and folder parameters to remove.
rm OPTIONS FILES_FOLDERS
- OPTIONS are optional where no, single or multiple options can be provided. Below we will examine most of the rm command options.
- FILE_FOLDERS is required where single or multiple file or folder should be provided to remove.
rm Command Options
rm command provides the following options and functions.
|-f , –force||Force deletion and remove write-protected files and folders|
|-i||Prompt before every removal of file or directory|
|-I||Prompt before every removal of 3 files or directories|
|-r , -R , –recursive||Remove files and folders recursively|
|-v , –verbose||Display deleted files and folders|
Single file can be easily deleted with the rm command easily. Just provide the file name if it is located current working directory or full path with the file name if it is located in different path than current working directory.
$ rm data $ rm ../data $ rm /home/ahmet/data
* can be used to specify the file name pattern which can be useful to specify extension of the file.
$ rm *.pdf $ rm ../*.txt $ rm /home/ahmet/*.txt
Remove/Delete Multiple Files
rm command can be also used to delete multiple files at the same time. Just put files by delimiting them with spaces.
$ rm data image tmp $ rm ../data ../../image tmp $ rm /home/ahmet/data /home/image /tmp
While removing multiple files we can also specify file named or extensions like below.
$ rm *.pdf *.tmp $ rm ../*.txt /tmp/*.pdf
rm command can be also used to remove or delete directory. But by default rm command without any option will not delete specified directory. In order to delete a directory with the rm command the
-r option which will delete recursively should be provided.
$ rm -r data $ rm -r ../data $ rm -r /home/ahmet/data
We can also specify directory name pattern to delete with the glob operator.
$ rm -r *tmp $ rm -r ../*tmp $ rm -r /home/ahmet/*tmp
We can also specify multiple directories in order to remove in a single command execution.
$ rm -r *tmp /home/ahmet/*tmp /var/tmp/*
Remove/Delete Files and Folders Recursively
rm command is designed to remove only specified file or directory. In order to delete or remove a directory and its contents without specifying one by one the recursive option should be used.
--recursive can be used for recursively delete files and directories.
$ rm -r tmp $ rm -r ../tmp $ rm -r /home/ahmet/tmp
Remove/Delete Files and Folders Forcibly
Linux files or directories can be write-protected in order to prevent change like adding new content to the files and directories. By default, these files and directories can not be deleted but using the force option
-f these write-protected files and directories can be deleted easily. Generally, the force options are used with the recursive option.
$ rm -f *.pdf $ rm -f ../*.txt $ rm -f /home/ahmet/*.txt $ rm -rf tmp $ rm -rf ../tmp $ rm -rf /home/ahmet/tmp
Remove/Delete Files and Folders Interactively with Confirmation
Deleting multiple files and directories can be a bit tricky where mistakes can be made which can not be reverted back. So deleting files and directories can be done step by step interactively by using
First ask for every removal of the file or directory with the
$ rm -r -i test
Display Deleted Files and Folders Verbosely
While deleting files and directories there will be no output about operations. Only the errors will be printed to the terminal. If you want to display, list or even log the delete operations and deleted files and directories the verbose options can be used with the
$ rm -r -v test