Operations 8 min read

Master Deleting Directories in Linux: rmdir, rm, and find Explained

This guide walks you through safely removing directories on Linux using rmdir for empty folders, rm for recursive deletions with various options, and find for pattern‑based removal, while highlighting required permissions, common pitfalls, and practical command‑line examples.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Deleting Directories in Linux: rmdir, rm, and find Explained

Introduction

Linux offers several ways to delete directories, from graphical file managers to command‑line utilities. When working on headless servers or needing to remove multiple directories at once, command‑line tools like rmdir, rm, and find are the most efficient.

Precautions

Using a desktop manager moves files to a trash bin, allowing recovery, but command‑line deletions are permanent. Ensure you have write permission on the target directory; otherwise you will encounter a “operation not permitted” error. Escape spaces in directory names with a backslash (\).

Deleting Empty Directories with rmdir

rmdir

removes only empty directories. Example: rmdir linuxmi If the directory is not empty, rmdir reports:

rmdir: failed to remove 'linuxmi': Directory not empty

In such cases, use rm or manually clear the contents first.

Deleting Directories with rm

rm

can delete both files and directories. By default it does not remove directories; use -d to delete an empty directory, or -r (or -R) for recursive deletion.

Example to delete a directory and all its contents: rm -r linuxmi To suppress confirmation prompts, add -f. To delete multiple directories at once: rm -r linuxmi linuxmi2 linuxidc Use -i for interactive confirmation, or -I to prompt once before proceeding. rm -rI linuxidc When deleting many files, you may see “Argument list too long”. This occurs because the number of arguments exceeds the system’s command‑line length limit.

Pattern‑Based Deletion with find

find

searches for files or directories matching expressions and can execute actions on each match. To delete all directories ending with _cache in the current directory: find . -type d -name '*_cache' -exec rm -r {} + Explanation of the options: . – start search in the current directory. -type d – restrict matches to directories. -name '*_cache' – match names ending with _cache. -exec rm -r {} + – run rm -r on the found directories.

Removing All Empty Directories

To delete every empty directory under a given path: find /dir -type d -empty -delete Options: /dir – starting point for the search. -type d – look for directories only. -empty – select empty directories. -delete – remove the matched directories (only works on empty ones).

Always place -delete as the last option and test the command without it first to avoid accidental data loss.

Handling Large Directories

If a directory contains many files, you can first delete all files with find and then remove the now‑empty directory: find /dir -type f -delete && rm -r /dir This avoids the “Argument list too long” error.

Conclusion

Using rm and find together provides powerful, flexible ways to delete directories based on various criteria. While the process is straightforward, always double‑check commands to prevent accidental loss of important data.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

rmfinddirectory deletionrmdir
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.