Signup/Sign In
LAST UPDATED: MARCH 17, 2022

How to Removes Empty Directories using rmdir Command

In Linux, if we want to remove empty directories, we use the rmdir command.

The rmdir (remove directory) rmdir command is used to remove an empty directory from the filesystem.

  • If the directory is not empty then we can not remove the directory by the rmdir command.
  • Be careful while removing a directory because when we remove a file or directory graphically then the directory is actually moved to the Trash and can be easily recovered but the directory removed by the rmdir command can not be recovered.

The general syntax of the rmdir

rmdir [OPTION]... DIRECTORY...

Brief description of options available with the rmdir command.

Options 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 this help and exit.
--version output version information and exit.

Example: Remove an empty directory using the rmdir command.

In this example, hope is an empty directory in the shadow parent directory using rmdir <directory name> you can delete an empty directory.

remove an empty directory linux

Example: Remove the child directory as well as the parent directory.

In this example, at a time we will delete the child directory as well as the parent directory using -p or --parents with rmdir command.

Remove the child directory as well as the parent directory.

Example: Use of --ignore-fail-on-non-empty option with rmdir command.

In this example, using this option does not report a failure that occurs solely because a directory is non-empty. You can see that shadow is a non-empty directory still no error reports.

Use of --ignore-fail-on-non-empty option with rmdir command

Conclusion

In this tutorial, we covered how we can remove empty directories from the file system with different options. If the specified directory has contents then we can not remove the directory using the rmdir command.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.