UNIX: Recursive Delete Directory / Files

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
Difficulty level Easy
Root privileges No
Requirements rm
Est. reading time N/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


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig 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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum