What Actually “rm -rf” Command Do in Linux? – Tecmint

The rm command is a UNIX and Linux command-line utility for deleting files or directories on a Linux system. In this article, we will clearly explain what the “rm -rf” command can actually do on Linux.

In addition, we will share some useful examples of how to delete a file, delete a directory, delete multiple files or directories, request confirmation, delete files recursively, and force file deletion.

The rm command is also one of the most commonly used commands on a Linux system, but also a dangerous command that you will discover later in this article.

How to delete a file in Linux

By default, the rm command only immediately deletes the file or files specified on the command line and does not delete directories.

$ mkdir -p tecmint_files$ touch tecmint.txt $ rm tecmint.txt $ rm tecmint_files <img src="https://www.tecmint.com/wp-content/uploads/2018/07/rm-command-example.png" alt="rm Command example

” />
rm Command example

How

to delete multiple files in

Linux

To delete multiple files at once, specify the file names one by one (for example:

file1 file2) or use a pattern to delete multiple files (for example: pattern ending with .txt) at once.

$ rm tecmint.txt fossmint.txt [Using file names] $ rm *.txt [Using pattern]

Delete multiple

files in Linux How to delete a directory

in Linux

To delete a

directory, you can use the -r or -R switch, which instructs rm to delete a directory recursively including its contents (subdirectories and files).

$ rm tecmint_files/ $ rm -R tecmint_files/

Delete directory on LinuxDelete
directory on Linux

How to delete files with confirmation message

To request confirmation when deleting a file, use the -i option as shown. $ rm -i

tecmint.txt

Delete files

with confirmation

How to delete directories

with confirmation message

To request confirmation when deleting a directory and its subdirectories, use the -R and -i options as shown.

$ rm -Ri tecmint_files/ <img src="https://www.tecmint.com/wp-content/uploads/2018/07/Remove-Files-with-Confirmation.png" alt="Delete directory with

confirmation” />
Delete

directory with confirmation

How

to forcibly delete file or directory

To delete files or directories forcefully, you can use the -f option to force a delete operation without rm by asking for confirmation. For example, if a file cannot be written, rm will ask you if you want to delete that file or not, to avoid this and simply execute the operation.

$ rm -f tecmint.txt

When you combine the -r and -f flags, it means that a directory (and its contents) are recursively and forcibly deleted without prompting.

$ rm -rf fossmint_files <img src="https://www.tecmint.com/wp-content/uploads/2018/07/Remove-Directory-with-Confirmation.png" alt="Force delete files and

directories” />
Force delete files and directories

How to display information during deletion

To display more information when deleting a file or directory, use the -v option, this will enable the rm command to display what is being done in the standard output.

$ rm -rv fossmint_files

Show deletion information

Learn rm -rf / Command You should always keep in mind that “

rm –

rf” is one of the most dangerous commands, which you can never execute on a Linux system, especially as root. The following command will erase everything on your root (/) partition.

# rm -rf /

Create alias for rm command on Linux

As a security measure, you can have rm always ask you to confirm a delete operation, whenever you want to delete a file or directory, using the -i option. To configure this permanently, add an alias in the $HOME/.bashrc file.

alias rm=”rm -i”

Save your changes and exit the file. Then, get your .bashrc file as shown or open a new terminal for the changes to take effect.

$ source $HOME/.bashrc

This simply implies that when you run rm, it will be invoked with the -i option by default (but using the -f flag will override this setting).

$ rm fossmint.txt $ rm tecmint.txt

Alias rm Command Confirmation
Alias rm Command ConfirmationDoes RM delete

a file?

Actually, the rm never erases a file, but is unlinked from the disk, but the data is still on the disk and can be recovered using tools like PhotoRec, Scalpel or Foremost.

If you really want to permanently delete the file or directory, you can use the purge command-line tool to overwrite a file and hide its contents.

That’s all! In this article, we’ve explained some examples of really useful rm commands and we’ve also elaborated on what the “rm -rf” command can do on Linux. If you have any questions or additions to share, please use the feedback form below to reach out to us.