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

last updated in Categories , , , ,

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?

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 15% off on Linux Foundation certified SysAdmin, Progamming, Kubernetes/Containers and Open Stack certification & course. Use "SPLASH15" coupon code. Offer expires on August 27, 2018
training.linuxfoundation.org

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Notable Replies

  1. You need to read good books on sysadmin. Another option is to read man pages for the command. If you still do not understand anything use our forum to post question. Study, read, learn, practice. That is all.

Continue the discussion www.nixcraft.com

1 more reply

Participants