UNIX: Recursive Delete Directory / Files

by on April 15, 2012 · 0 comments· LAST UPDATED April 15, 2013

in , ,

How do I delete (remove) a directory called $HOME/foo and its content including all files and sub-directories under Unix like operating systems? What is the best way to completely delete /tmp/foo/ with all it's content?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementsrm
Estimated completion timeN/A
You need to use the rm command line utility to remove the files (and directories) specified on the command line. The following command will delete everything including subdires and file from the $HOME/foo directory. So be careful. You will not able to recover files or sub-dirs from the $HOME/foo directory. You have been warned. In order to delete directories, it is necessary to use the -r or -R option. This option recursively removes directories and their contents in the argument list passed to the rm command. The user is normally prompted for removal of any write-protected files in the directories unless the -f option is used by the end user. The syntax is as follows:

 
rm -rf /path/to/dir
rm -rf $HOME/foo/
rm -rfi $HOME/foo/
 

Where,

  1. -r or -R : Attempt to remove the file hierarchy rooted in $HOME/foo/ i.e. delete all directory's contents including all files and sub-dirs.
  2. -f : Attempt to remove the files without prompting for confirmation, regardless of the file's permissions.
  3. -i : Request confirmation before attempting to remove each file, regardless of the file's permissions
See also

For more information, see the rm command man page by typing the following command:
$ man rm

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: , , , , , , , , , , , ,

Previous Faq:

Next Faq: