rm -rf Command in Linux With Examples
Last Updated :
10 Oct, 2024
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 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 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_*

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

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

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

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.

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.

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 /

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.

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.
Similar Reads
rm command in Linux with examples
rm stands for remove here. rm command is used to remove objects such as files, directories, symbolic links and so on from the file system like UNIX. To be more precise, rm removes references to objects from the filesystem, where those objects might have had multiple references (for example, a file w
5 min read
rmmod command in Linux with Examples
The rmmod command in Linux is used to remove or unload a module from the kernel. Modules are pieces of code that can be dynamically loaded into the Linux kernel to extend its functionality, such as drivers for hardware devices. When a module is no longer needed, it can be removed using rmmod. Althou
3 min read
rmdir Command in Linux With Examples
In Linux, managing directories efficiently is a crucial skill for both system administrators and everyday users. The 'rmdir' command is a specialized tool designed specifically for removing empty directories, helping to maintain a clean file system and avoid accidental data loss. This guide delves i
6 min read
rev command in Linux with Examples
rev command in Linux is used to reverse the lines characterwise. This utility reverses the order of the characters in each line by copying the specified files to the standard output. If no files are specified, then the standard input will be read. Here, we will explore the syntax, examples, and opti
3 min read
rcp Command in Linux with examples
When working in a Linux environment, there often comes a time when you need to transfer files from one computer to another. While more secure options like scp or rsync exist, the rcp (Remote Copy Protocol) command offers a simple and efficient way to copy files between systems, especially for beginn
5 min read
ln command in Linux with Examples
The 'ln' command in Linux is a powerful utility that allows you to create links between files. These links can either be hard links or soft (symbolic) links. If you're unfamiliar with these concepts, check out our detailed guide on Hard and Soft Links in Linux to understand their differences, use ca
3 min read
printf command in Linux with Examples
The 'printf' command in Linux is a versatile tool used to display formatted text, numbers, or other data types directly in the terminal. Like the 'printf' function in programming languages like C, the Linux printf command allows users to format output with great precision, making it ideal for script
4 min read
mkfs Command in Linux with Examples
The mkfs command stands for âmake file systemâ and is utilized to create a file system, which organizes a hierarchy of directories, subdirectories, and files, on a formatted storage device. This can be a partition on a hard disk drive (HDD), a USB drive, or other storage media. A partition is a logi
5 min read
read command in Linux with Examples
read command in the Linux system is used to read from a file descriptor. This command reads up the total number of bytes from the specified file descriptor into the buffer. If the number or count is zero, this command may detect errors. But on success, it returns the number of bytes read. Zero indic
3 min read
shuf Command in Linux with Examples
The shuf command in Linux writes a random permutation of the input lines to standard output. It pseudo randomizes an input in the same way as the cards are shuffled. It is a part of GNU Coreutils and is not a part of POSIX. This command reads either from a file or standard input in bash and randomiz
4 min read