Removing directories in Linux is a common task when managing files and organizing your file system. Linux provides two primary commands for this task: rm and rmdir. Each command serves a different purpose, and knowing when to use each can help you avoid unintended data loss or errors.
Removing Directories with the rm Command
The rm command is versatile and allows you to delete both files and directories. When deleting directories, you must use the -r (recursive) option, which tells the command to remove directories along with all their contents.
rm -r directory_name
Replace directory_name with the name of the directory you want to remove. For example, to delete a directory named myfolder, you’d run:
rm -r myfolder
-f (force) option:rm -rf myfolder
Be very careful using rm -rf, as it deletes directories and files permanently without any recovery option.
rm -r dir1 dir2 dir3
-i (interactive) option, which prompts for confirmation before removing each file or subdirectory:rm -ri directory_name
Join readers who trust AllThings.How
Add us as a preferred source on Google so our practical guides show up first next time you search.
Add to Google Preferences →Removing Empty Directories with the rmdir Command
The rmdir command is specifically designed to remove empty directories. It is safer than rm because it prevents accidental deletion of directories containing files or subdirectories.
rmdir directory_name
For example, to delete an empty directory named emptyfolder, enter:
rmdir emptyfolder
rm -r command instead.rmdir emptydir1 emptydir2 emptydir3
rmdir -p parentdir/childdir
This command will remove childdir and, if parentdir becomes empty afterward, it will remove parentdir as well.
Deleting Directories Based on Patterns with find and rm
Sometimes, you may need to delete directories based on specific patterns or criteria. The find command combined with rm provides a powerful way to accomplish this.
find . -type d -name 'pattern' -exec rm -r {} +
For example, to delete all directories ending with _backup in the current directory, run:
find . -type d -name '*_backup' -exec rm -r {} +
find /path/to/directory -type d -empty -delete
Replace /path/to/directory with the actual path you want to search.
find command without the -delete or -exec rm option first.Resolving “Argument List Too Long” Errors
If you attempt to delete a directory containing a very large number of files, you might encounter the “Argument list too long” error. To handle this, use the find command to delete files individually before removing the directory:
find /path/to/directory -type f -delete && rm -r /path/to/directory
This command deletes all files within the directory first, then removes the directory itself.
Deleting directories in Linux is straightforward once you understand the differences between rm and rmdir. Always exercise caution, especially with recursive and forceful deletion options, to avoid unintended data loss. Always make sure you have backups of important data before performing bulk deletions.






