rmdir

file managementlinux
The rmdir command is one of the most frequently used commands in Linux/Unix-like operating systems. rmdir The rmdir command is used to remove empty directories from the filesystem in Linux/Unix systems. rmdir stands for "remove directory" and is a safe way to delete directories as it will only work if the directory is empty.

Quick Reference

Command Name:

rmdir

Category:

file management

Platform:

linux

Basic Usage:

rmdir empty_directory

Common Use Cases

  • 1

    Directory removal

    Remove empty directories from the filesystem

  • 2

    Cleanup

    Remove unnecessary or temporary directories

  • 3

    Scripting

    Use in shell scripts to remove directories programmatically

  • 4

    File management

    Manage directories and files in the filesystem

Syntax

rmdir [OPTION]... DIRECTORY...

Options

Option Description
--ignore-fail-on-non-empty Ignore each failure that is solely because a directory is non-empty
-p, --parents Remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is similar to 'rmdir a/b/c a/b a'
-v, --verbose Output a diagnostic for every directory processed
--help Display help information and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the rmdir command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

rmdir documents

Removes the empty directory named documents from the current working directory. This will fail if the directory contains any files or subdirectories.

rmdir dir1 dir2 dir3

Removes multiple empty directories at once. This example removes three empty directories named dir1, dir2, and dir3.

rmdir -p parent/child/grandchild

Removes directories and their parents recursively. The -p option removes the specified directory and then attempts to remove each parent directory if they become empty. In this example, it will remove grandchild, then child, and finally parent if each becomes empty after the previous removal.

rmdir --ignore-fail-on-non-empty test_dir

Attempts to remove a directory, but doesn't display an error if the directory contains files. This is useful in scripts where you want to attempt a removal but continue regardless of the outcome.

Advanced Examples:

rmdir -pv project/src/lib

Removes nested directories with verbose output. The -v option displays a message for each directory processed. Combined with -p, it will show the removal of lib, then src, then project (if each becomes empty).

rmdir "Old Project Files"

Removes a directory with spaces in its name. When directory names contain spaces, they should be enclosed in quotes to be properly interpreted.

find ./temp -type d -empty -exec rmdir {} \;

Uses find with rmdir to remove all empty directories under ./temp. This is a powerful combination to clean up empty directories in a directory tree.

mkdir -p test/{a,b,c}/{1,2,3} && rmdir -p test/{a,b,c}/{1,2,3}

Creates a complex directory structure and then removes it completely. This demonstrates how rmdir -p can clean up an entire directory tree of empty directories.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Important Constraints:

  • The rmdir command only removes empty directories
  • If a directory contains files, hidden files, or subdirectories, rmdir will fail
  • To remove directories that contain files, use rm -r instead (but use with caution)
  • The command will fail if you don't have appropriate permissions for the directory

Common Use Cases:

  • Cleaning up empty directories after moving or deleting their contents
  • Removing temporary directory structures after completing a task
  • Safely removing directories when you want to ensure they're empty first
  • Removing multiple nested directories in a controlled manner

Tips:

  • Use the -p option to remove empty parent directories at the same time
  • Combine with find to remove all empty directories in a directory tree
  • If you need to remove directories that contain files, use rm -r instead
  • Use --ignore-fail-on-non-empty in scripts where you want to attempt removal but continue if it fails
  • The -v option is helpful for debugging and seeing exactly what directories are being removed

Related Commands:

  • rm -r - Remove directories and their contents recursively
  • mkdir - Create directories
  • find - Search for files/directories and perform actions on them
  • ls - List directory contents to check if a directory is empty

Tips & Tricks

1

Use the -p option to remove parent directories if they become empty

2

Use the -v option to display a message for each removed directory

3

Use the -ignore-fail-on-non-empty option to ignore errors for non-empty directories

4

Use the --help option to display help

5

Use the --version option to display version information

Common Use Cases

Directory removal

Remove empty directories from the filesystem

Cleanup

Remove unnecessary or temporary directories

Scripting

Use in shell scripts to remove directories programmatically

File management

Manage directories and files in the filesystem

Security

Prevent unauthorized access to sensitive directories

Related Commands

These commands are frequently used alongside rmdir or serve similar purposes:

Use Cases

1

Directory removal

Remove empty directories from the filesystem

2

Cleanup

Remove unnecessary or temporary directories

3

Scripting

Use in shell scripts to remove directories programmatically

4

File management

Manage directories and files in the filesystem

5

Security

Prevent unauthorized access to sensitive directories

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the rmdir command works in different scenarios.

$ rmdir
View All Commands
rmdir - Linux Command Guide