The rmdir command in Linux: delete empty directories

Introduction

In the day-to-day administration of Linux systems, one of the most frequent tasks is cleaning the filesystem by removing directories that are no longer used. When a directory is empty, the most suitable and safe tool is rmdir. This command is designed exclusively to delete directories that contain no files or subdirectories, thus avoiding accidental deletion of important data.

What is rmdir?

The name rmdir comes from the English words remove directory. Unlike rm -r, which can delete directories and their contents recursively, rmdir only acts when the target is completely empty. If the directory contains any element, the command returns an error and performs no action, providing an extra layer of protection.

Basic Syntax

The simplest way to use rmdir is:

rmdir [options] directory_name

Where directory_name is the relative or absolute path of the directory to be removed. If the path is omitted, the command assumes the directory is in the current working directory.

Common Options

  • -p: removes the directory and its empty parents recursively upward. For example, if you run rmdir -p a/b/c and a/b and a become empty after removing c, they will also be removed.
  • -v or --verbose: prints an informative message for each directory removed, useful in scripts or when you want to confirm the action.
  • --help: displays brief help for the command.
  • --version: shows the version of rmdir installed.

Practical Examples

Suppose we have the following structure in the user’s home:

/home/user/projects/old/

The directory old is empty. To delete it, simply run:

rmdir /home/user/projects/old

If we want to delete several empty directories in one line, we can list them separated by spaces:

rmdir dir1 dir2 dir3

If we wish to delete a directory and its empty parents, we use the -p option:

rmdir -p /home/user/projects/old/subdir

This will delete subdir and, if after that old becomes empty, it will also be deleted, and so on upward as long as the parents remain empty.

To see what is being done, add the --verbose option:

rmdir -vp /home/user/projects/old/subdir

Expected output:

removed directory: /home/user/projects/old/subdir\nremoved directory: /home/user/projects/old

Precautions and Best Practices

  • Always verify that the directory is really empty before running rmdir. You can use ls -la path to confirm.
  • Avoid using wildcards like * without checking first; for example, rmdir * will attempt to delete all empty directories in the current path, but if any is not empty the command will fail and show an error.
  • In administration scripts, combine rmdir with a simple test: if the directory exists and contains nothing, then delete it.
  • Remember that rmdir does not delete files or directories with content; for those cases use rm -r with extreme caution.

Alternatives When the Directory Is Not Empty

If you need to delete a directory that contains files, you have several options:

  • rm -r directory_name: recursively deletes all contents. Use the -i flag to request confirmation before each deletion.
  • rm -rf directory_name: forces deletion without prompting; use it only when you are absolutely sure.
  • Use find to delete specific files and then apply rmdir to the now-empty directory.

In production environments, many administrators prefer to move the directory to a temporary trash (mv to /tmp/trash/) and review its contents before deleting it permanently.

Conclusion

The rmdir command is a simple yet powerful tool for keeping the filesystem clean and orderly. Its limitation to empty directories makes it a safe option against accidental deletions. By knowing its syntax, options, and best practices, you can incorporate it effectively into your Linux administration routines, cleanup scripts, and daily maintenance tasks.

This post is also available in ESPAÑOL.

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .