[Linux] Safely Removing Empty Directories with the rmdir Command

目次

Overview

rmdir (remove directory) is a command specifically designed to delete empty directories.

If you attempt to delete a directory that still contains files, the command will encounter an error and stop. This eliminates the risk of accidentally deleting important data. It is primarily used in safety-critical situations where you want to ensure a folder is truly empty before removal, or for cleaning up empty folders within a script.

While rm -r can be used to force-delete a directory along with its contents, rmdir serves as a vital safety mechanism.


Specifications (Arguments and Options)

Syntax

rmdir [options] [directory_name...]

Main Options

OptionDescription
-pDeletes the specified directory, and if the parent directory becomes empty as a result, deletes the parent as well (parents).
-vDisplays each directory as it is processed (verbose).
--ignore-fail-on-non-emptySuppresses error messages if a directory is not empty and cannot be removed (useful for error handling in scripts).

Basic Usage

Deleting an Empty Directory

The most basic usage is specifying the name of the empty directory you wish to remove.

# Delete the empty directory named "empty_folder"
rmdir empty_folder

Execution Result Example:

(Nothing is displayed on success; an error is shown on failure.)

(No output)

Practical Commands

1. Deleting Deep Hierarchies Simultaneously (-p)

If you have a structure like logs/2026/01 and all of them are empty, specifying the lowest level with the -p option will remove the parent directories in a chain.

# Deletes the '01' directory, then '2026', then 'logs' if they become empty
rmdir -p logs/2026/01

2. Displaying the Deletion Progress (-pv)

Combining the -p and -v options visualizes exactly which directories are being removed, making the behavior easier to track.

rmdir -pv project/temp/cache/v1

Execution Result Example:

rmdir: removing directory, 'project/temp/cache/v1'
rmdir: removing directory, 'project/temp/cache'
rmdir: removing directory, 'project/temp'
rmdir: removing directory, 'project'

3. Ignoring Failures if Not Empty

This is useful for scripts where you want to delete a directory if it happens to be empty but leave it intact if it contains files, without stopping the script due to an error.

# If the directory contains files, it remains untouched and no error is shown
rmdir --ignore-fail-on-non-empty user_data_dir

Customization Points

  • Multiple Directories: You can delete multiple directories at once by separating them with spaces: rmdir dir1 dir2 dir3.
  • Paths: You can use both absolute paths (/tmp/work) and relative paths.

Important Notes

  • Cannot Delete Files: If even one file remains in the directory, you will receive an error: rmdir: failed to remove 'xxx': Directory not empty. This is a deliberate safety feature.
  • Beware of Hidden Files: Even if a folder looks empty, hidden files like .DS_Store (macOS) or .gitignore will prevent rmdir from working. Use ls -a to verify.
  • Parent Permissions: To delete a directory, you need write permissions for the parent directory, not just the directory itself.

Advanced Application

Combining with the find Command to Clear Empty Directories

For cleaning up a large number of empty directories across a system, combining rmdir with the find command is the modern approach.

# Search for and delete all empty directories within the current directory
find . -type d -empty -delete

Conclusion

The rmdir command is the “safe janitor” of Linux.

Reminder: For directories containing files, you must use rm -r.While rm -rf is powerful and dangerous, rmdir protects you from accidents where you might have thought a folder was empty when it actually contained important files.

Best Use Cases: Cleaning up empty folders after processing or during cautious operations to prevent accidental deletion.

Key Adjustments: Use -p for hierarchies and -v to monitor progress.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次