What does rm -rf command do on a Linux or Unix

I am a new Linux sysadmin. What is the purpose of rm -rf command on a Linux? What does rm -rf command do on a Unix-like operating system?

The rm command removes each given FILE on the command line. By default, it does not remove directories. But, you can force it to remove directories too. So what does rm -rf command do on a Linux or Unix cli?
Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements None
Time N/A

ADVERTISEMENTS

What does rm -rf command do on a Linux or Unix?

The syntax to delete file for rm command is as follows:
rm file
To delete five files named file1, file2, file3, file4, and file5, run:
rm file1 file2 file3 file4 file5
To display the name of each file before removing it:
rm -v file
Use the ls command to list files:
ls -l
ls -ld dirname

To list all files and directories for given directory name, try find command:
find dirname
find /path/to/dir/ -ls
find /path/to/dir2/ -type d ls
find /path/to/dir2/ -type f ls

Remove the listed directories and their contents recursively

To delete all files and its directories, run:
rm -r dir1
rm -r -v dirname2

You can force the rm command to ignore nonexistent files and missing operands, and never prompt the user:
rm -r -f myDirectory
rm -r -f -v /path/to/dir2/

Sample session:
What does rm -rf command do on linux or unix

Be careful with rm -rf / command

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Unix and Linux disasters. The rm -rf / variant of the command, if run by an administrator or root user, would cause the contents of every writable mounted filesystem on the computer to be deleted. Do not try these commands on Linux or Unix like system:
rm -rf /
sudo rm -rf /
sudo rm -rf *

Make a backup

It is critical that you make a backup of all essential data. The data backup must be verified from time to time. Backups allow you to recover data from wrongly typed rm or rmdir commands.

How to prompt whether to remove each file with rm

Pass the -i or -I as follows:
rm -i fileNameHere
rm -i -r dirName
rm -I fileName

The -I option to the rm command prompt once whether to proceed with the command, if more than three files are named or if a recursive removal is requested. Ignore any previous -f option.

rm vs rmdir command to delete directories

The rm command can delete both files and directories (empty or non-empty). The The rmdir command can only remove empty directories. The syntax for rmdir command as follows:
rmdir dirnamehere
rmdir -v dir1

To delete non-empty directories try the rm command:
rm -rf dirName

Getting help on rmdir command

Type the following command at the shell prompt:
man rmdir
rmdir --help

Sample outputs:

Usage: rmdir [OPTION]... DIRECTORY...
Remove the DIRECTORY(ies), if they are empty.
 
      --ignore-fail-on-non-empty
                  ignore each failure that is solely because a directory
                    is non-empty
  -p, --parents   remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is
                    similar to 'rmdir a/b/c a/b a'
  -v, --verbose   output a diagnostic for every directory processed
      --help     display this help and exit
      --version  output version information and exit

Getting help on rm command

Issue the following man command:
man rm
rm --help

rm -rf command summary

what does rm -rf command do on a Linux or Unix like system:

  1. -r : Remove directories and their contents recursively
  2. -f : Force removal of files/directories. Ignore nonexistent files and arguments, never prompt for confirmation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.