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:
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
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
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:
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:
Deleting folder recursively command summary
|Command and options||Description|
|-f||Forceful option. Ignore nonexistent files and arguments, never prompt|
|-r||remove directories and their contents recursively|
|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:
🐧 8 comments so far... add one ↓
|Category||List of Unix and Linux commands|
|Firewall||Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04|
|Network Utilities||dig • host • ip • nmap|
|OpenVPN||CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04|
|Package Manager||apk • apt|
|Processes Management||bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time|
|Searching||grep • whereis • which|
|User Information||groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w|
|WireGuard VPN||Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04|