Open In App

rm -rf Command in Linux With Examples

Last Updated : 10 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

rm command in UNIX stands for remove and by default is used for removing files. It can also be used as an alternative to rmdir command which is primarily used for removing empty directories by using –d option but is mainly used with options such as -rf which allow it to delete non-empty directories forcefully. In this article, we will see how rm command works with the -rf option along with some examples and other options that the command provides.

Syntax

rm --[options] foldername

Use double hyphens — for verbose (long-form) options and single hyphens – for short-form options. You can also use multiple options in a single command by appending each option at the end of the previous option like -rf option rather than using two separate commands with -r and -f options.

Make sure to use the short form notation if you are clubbing multiple options. To know more about rm command, one can use the –help option which lists all the available options you can use with the rm command.

help command

help option for remove

Common Options used with rm

The most commonly used options for the rm command which is used with the above syntax are as follows:

Option

Verbose

Description

-r

–recursive

Used to delete a folder or directory since rm command deletes a file by default.

-f

–force

Remove a file or folder even if it is write-protected or does not exist.

-d

–dir

Alternative to rmdir command to remove empty directory,

-i

–interactive

Asks the user for confirmation before removing the file or folder.

1. Removing Files in Linux

The rm command, by default, cannot remove Directories and only works on files. 

touch myfile.txt
mkdir myfolder
ls
rm error command

rm command throwing error

We use mkdir and touch commands to make directories and text files respectively, and ls command to list files in the current working directory.

2. Removing Multiple Files in Linux

To remove multiple files at once, we can write file names separated by spaces after the rm command or use a pattern to remove multiple files that fit the pattern. We have created four files named a_file.txt, a_java_file.java, a_python_file.py, another_file.txt. We are trying to remove files starting with “a_” using the command rm a_*

rm a_*
rmwildcard

rm wildcard working

3. Removing a Directory in Linux

To remove a directory, you can use the -r or -R switch, which deletes a directory recursively including its content (subdirectories and files). If it is an empty directory, you can also use rmdir command or rm -d command. The meaning of recursive is that the command will look for the subfolder which does not have more folders inside them and has only files and starts removing the files one by one.

After all the files has been removed from the sub folder, the rm command removes the empty directory and starts to look for more such sub folder. It will keep on doing this step until the parent directory becomes empty and removes that too. Hence it follows a bottom-up approach.

rm -r myfolder
rmdirectory

Remove non-empty directory

Note: We have installed a package named tree to list down directories and subdirectories using sudo apt install tree

4. Removing Files and Directories with Confirmation Prompt

To get a confirmation prompt while deleting a file, use the -i option.

rm -i myfile.txt
rm -ri myfolder
rmiexample

Confirmation asked by using -i option

5. Removing File or Directory Forcefully

To remove a file or directory forcefully, you can use the option -f force a deletion operation without rm prompting you for confirmation. For example, if a file is unwritable, rm will prompt you whether to remove that file or not, to avoid this and simply execute the operation. It will also not throw an error even if it does not the specified file or folder.

rm -f myfile.txt
rmrfusage

rm -f usage for removing a file forcefully

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

rm -rf myfolder

Here, we created a text file and directory containing another file and made it read-only by taking its write access using chmod command.

rmrffusage

rm -rf usage for removing folder forcefully

Note: The folder must have the write permission, and the individual files stored inside the directory may or may not need the write permissions.

Is rm -rf Command bulletproof?

rm -rf as powerful as it is, can only bypass read-only access of nested files and not directories. To delete the directory ( B/C ) we need to access it through superuser privileges.

Note: It is not recommended to use this command as a superuser if you are not 100% sure what you are doing as you can delete important files.

sudormexample

Removing write-protected folder using privileges

The “rm -Rf /” Command

You should always keep in mind that “rm -rf” is one of the most dangerous commands, that you should never run on a Linux system, especially as a root. The following command will clear everything on your root (/) partition.

sudo rm -rf /

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

There are checks to prevent root deletion but the additional option of –no-preserve-root bypass that failsafe. It’s a meme on the internet that is equivalent to deletion of system32 in your windows os C:\ drive.

sudo rm -rf / --no-preserve-root

You should not use the above command in any case whatsoever, for curious folks I did it use the command with –no-preserve-root.  And after some deletion of important files and directories, I was left with nothing but hanged up output shown below.

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

Conclusion

In this article, we have discussed the importance of remove command to remove files and folders with examples and presented the common options such as recursive, force used along with this command to remove non-empty directories. We have also seen the risks of remove command if it is used on root directory with super user privileges which results in damaging the linux OS.



Next Article

Similar Reads