Linux rmdir Command: How to Remove Empty Directories

The rmdir command in Linux removes empty directories. It leaves directories containing files or subdirectories untouched, so you can remove empty directories while preserving their contents.

Deleting Empty Directories

Specify the path of the directory to remove. Relative paths are resolved from the current working directory, so check the location and target with pwd and ls -la before running the command.

rmdir empty-dir

An empty directory is normally removed without output. If it contains files or subdirectories, it is not removed and an error such as Directory not empty appears. Error messages depend on the system language. Removed directories are not normally moved to the trash, so confirm the target first.

If you specify multiple directories, it will attempt to delete each one. Even if some fail because they are not empty, other targets may still be deleted, so you should not think of this as an operation that either completely succeeds or is completely canceled.

rmdir first-dir second-dir

Try it yourself

Carry out the following practice in a location that does not overlap with existing files. If rmdir-practice already exists, choose another name, and if directory creation fails, do not execute the next command. If you use a different name, replace rmdir-practice in the commands below with that name.

mkdir rmdir-practice

If the creation is successful, enter the practice directory with the following command and create directories that are empty and directories that contain files.

cd rmdir-practice
mkdir empty busy
touch busy/example.txt

empty is empty, and busy contains files. Try deleting both directories in order.

rmdir empty
rmdir busy

The first command deletes empty. The second fails because busy is not empty, and the files remain. You can use the next command to check even hidden items.

ls -la busy

. and .. in the output refer to the current and parent directories. If there are other files or subdirectories, it is not empty. The following -p practice is also conducted in this directory. When removing practice files, check the target first.

-p option to delete up to the parent directory

-p or --parents tries to delete the directories from the specified directory up to the parent directories in order. Each directory must be empty at that time. You can check it in the practice directory as follows.

mkdir -p a/b/c
rmdir -p a/b/c

If there is no other content, they will be deleted in the order c, b, a. If there are other files in the parent directory, deleting the lower directories might succeed but deleting the parent directory may fail. Therefore, make sure the entire path is intended for deletion before running. After finishing the practice, return to the parent directory with cd ...

Display operations and ignore non-empty directories

-v or --verbose displays messages about the directory being processed. Check error messages to determine whether the deletion actually happened.

rmdir -v empty-dir

GNU rmdir's --ignore-fail-on-non-empty ignores the deletion failure that occurs because the directory is not empty. The directory or its contents are not deleted. The following command is executed from the parent location of the practice directory.

rmdir --ignore-fail-on-non-empty rmdir-practice/busy

This option only ignores directories if they are not empty. It is not an option that ignores other errors such as missing paths or lack of permissions. If only ignored failures occur, the exit status may be shown as successful, so do not judge that all directories have been deleted based solely on the exit status. You can check the detailed behavior of the option in the GNU rmdir manual.

Checking when deletion fails

  • Not empty: Replace directory with the actual path in ls -la directory to check hidden files and subdirectories. In common ls, items whose names start with a dot may not be visible.
  • Permission issue: Check the permissions, ownership, and filesystem status of the target and its parent directory. Deletion generally requires write and search permissions for the parent directory, and other restrictions may also apply.
  • Path or target issue: Check the name and current location. A symbolic link pointing to a directory is different from the directory itself, so check with ls -ld.

rmdir * allows the shell to pass multiple names in the current directory to the command. Files may also be included, and hidden names are usually not included, so it is safer to first check the target with ls -la and specify the necessary directories. If you need to delete a directory that contains files, you cannot do it with rmdir, and file deletion should be decided separately after reviewing the contents.