If you are managing a Linux system, you may sometimes need to delete folders that contain files and subdirectories. The rm command is the simplest and most powerful way to remove both empty and non-empty directories from the command line.
This guide explains how to safely delete non-empty directories using the rm command and its different options.
The general syntax of the rm command is:
rm [option] FileOrFolderName
By default, the rm command is used to delete files. However, with the right options, you can also remove directories and their contents.
To delete a directory along with all its files and subdirectories, use the -r (recursive) option:
rm -r Simple-Directory
This command removes the directory and everything inside it.
⚠️ Caution:
The rm -r command permanently deletes all files and subdirectories within the specified directory. Ensure you have a backup if you need to restore data later.
When you try to delete a write-protected directory, Linux will ask for confirmation before removing each file.
To skip these prompts and force delete the directory, use the -rf option:
rm -rf Simple-Directory
Here:
This command is powerful, so use it with care—especially when logged in as the root user.
You can also delete empty directories using the -d option:
rm -d Empty-Directory
This works similarly to the rmdir command, but gives you more flexibility since rm can handle both empty and non-empty directories.
The rm command can remove multiple directories in a single command.
Simply list all the directory names separated by spaces:
rm -r Directory_1 Directory_2 Directory_3
This will recursively delete all the listed directories and their contents.
| Action | Command | Description |
| Delete non-empty directory | rm -r directory_name | Removes directory and all contents |
| Force delete without confirmation | rm -rf directory_name | Deletes everything without prompts |
| Delete empty directory | rm -d directory_name | Removes only empty folders |
| Delete multiple directories | rm -r dir1 dir2 dir3 | Deletes several folders at once |
Always double-check your directory path before running the rm -rf command. A small typo can lead to loss of important files. If you want to test before deleting, use the ls command to list the contents first.
If you are managing Linux servers and want a secure and reliable hosting environment, Host.co.in offers Linux VPS Hosting and Dedicated Servers with 24/7 expert technical support, 99.95% uptime, and Tier IV Indian data centres.
Explore Linux VPS Hosting Plans