Linux distributions provide the rm
command in order to remove files and directories. The rm command provides a different option which rm -rf
is one of the most popular of them. By default, the rm command without any option does not delete a directory with contents. But the “rm -rf” command is used to delete a directory event it has child directories or files. In this tutorial, we examine the “rm -rf” command in detail.
rm Command
The rm command is used to remove files and directories. We can use the rm command without any option to remove single or multiple files. In the following example, we delete the file named “db.txt”.
$ rm db.txt
In the following example, we delete multiple files. The files with the “*.txt” extension will be deleted with the following command.
$ rm *.txt
Force Remove
Some times some files do not exist those are provided to the rm command. This situation results in a command prompt but we can force the removal without any prompt by forcing. The -f
option is used to force remove.
$ rm -f /home/ismail/*.txt
Remove Recursively
The rm command removes single or multiple files or specified directories by default. It does not remove child files and directories for the specified directory. We should use the -r
recursive option in order to remove all files and folders for the specified directory.
“rm -rf” Command
The -f
and -r
options are used to create the rm -rf
command. The “rm -rf” command simply removes all files and directories for the specified directory. The “rm -rf” command is dangerous and it should be used cautiously.
$ rm -rf /home/ismail/data
“rm -rf *” Command
The rm -rf *
command is derived from the “rm -rf” command. The *
sign is used to specify all files and directories for the current working directory. Simply the “rm -rf *” command deletes all files and directories for the current working directory.
$ rm -rf *
“rm -rf” Alias
If you are using the “rm -rf” command heavily for daily operations it can be useful to create an alias to call it in a practical way. The alias
command is used with the alias name. The alias should be added to the user’s ~/.bashrc
file to make it permanent. Add the following line to the ~/.bashrc file. We create an alias named rm-all
.
alias rm-all = "rm -rf"