Linux Delete Folder Recursively Command

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 to delete folder recursively under Linux.

rm command syntax to delete directories recursively

The syntax is as follows:

Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time 2m
rm -r dirName
## OR ##
rm -r folderName
## OR ##
rm -rf folderName

Did you know?
Everything is a file in Linux and Unix-like systems. In other words, your pictures, documents, directories/folders, SSD/hard-drives, NIC, USB devices, keyboards, printers, and some network communications all are files.

Examples that examples how to delete folder recursively

In this example, recursively delete data folder in the current home directory:

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

We can add the -v option to see verbose outputs. In other words, the rm command will explain what is being done to our files and folders on Linux. For instance:
rm -rfv /path/to/dir1
rm -r -f -v /home/vivek/oldpartpics

Removing folders with names containing strange characters

Your folders and files may have while spaces, semicolons, backslashes and other chracters in Linux. For example:
ls -l
Let us say we have a folder named “Our Sales Data” and “baddir#” or “dir2 ;#“. So how do we delete those directories with special names containing strange characters? The answer is simple. We try to enclose our troublesome filename or folder name in quotes. For example:
rm 'Our Sales Data'
rm -rfv '/path/to/Dir 1 ;'
rm -r -f -v "baddir#"
rm a\ long \dir1 \name

Sometimes, we need insert a backslash ( \ ) before the meta-character in your filename or folder name:
rm \$dir1
Linux Delete Folder Recursively Command

Deleting folder recursively command summary

rm command options for removing dirs/folders recursively
Command and options Description
-f Forceful option. Ignore nonexistent files and arguments, never prompt
-r remove directories and their contents recursively
-v Verbose output
rm -- '-dir1' Remove a dir/file whoes name start with a ‘-
rm ./-dir1 Same as above
rm -rfv 'dir name here' Enclose your troublesome filename/folder in quotes
rm -rfv \$dirname1 Same as above

See Linux rm(1) command man page or rm command example page for more information:
man rm
rm --help


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

🐧 8 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
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 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
8 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)
    • WP Dev Jan 31, 2021 @ 6:02

      You can combine -f, -i, -v and all other such options per your needs. we can see a list of all options by running bash command
      rm --help

  • tuunga Jul 9, 2013 @ 13:42

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

    • script Jan 30, 2021 @ 22:09

      you have to use “rm -rf dirName” if you not want that system ask you 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?

    • WP Dev Jan 31, 2021 @ 5:59

      cd into /home/
      cd /home/
      ls -l

      Then go to /wp-content/cache/:
      cd /wp-content/cache/
      rm -rfv *

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