Linux Delete Folder Recursively

How do I delete folder recursively under Linux operating systems using a bash command line options?

You need to use the rm command to remove files or directories (also known as folders) recursively. The rmdir command removes only empty directories. So you need to use rm command.

ADVERTISEMENTS

rm command syntax to delete directories recursively

The syntax is as follows:[donotprint]

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Time2m
[/donotprint] rm -r dirName

OR

rm -r folderName

OR

rm -rf folderName

Examples

In this example, recursively delete data folder:

rm -r /home/vivek/data/

The specified /home/vivek/data/ will first be emptied of any subdirectories including their subdirectories and files and then data directory removed. The user is prompted for removal of any write-protected files in the directories unless the -f (force) option is given on command line:

rm -rf dirname-here

OR

rm -r -f /path/to/folder/

To remove a folder whose name starts with a -, for example '--dsaatia', use one of these commands:

rm -rf -- --dsaatia

OR

rm -rf ./--dsaatia

See Linux rm(1) command man page or rm command example page for more information.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
5 comments… add one
  • Thota Madhu Sudhan Mar 11, 2013 @ 18:11

    What is the significance of -r in the code ?
    rm -r dirname
    what is the difference in using -rf and -r ?

    • K-hole Mar 18, 2014 @ 14:34
      • -r :recursive
      • -f :force (no prompt)
  • tuunga Jul 9, 2013 @ 13:42

    # rm -r dirName
    on centos ask every file to be deleted

  • Miloslav Havrda Jan 22, 2016 @ 11:54

    Thank you very much.

  • Dima Feb 10, 2016 @ 1:21

    Thanks. Great article.
    I have a question. How can I delete all subfolders /wp-content/cache/* in all folders in /home/ directory?

Leave a Reply

Your email address will not be published.

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