rmdir Command in Linux – Usage + Examples

Last updated: March 15, 2022

In Linux, directory is a file that stores file names and their information. From time to time unwanted directories have to be removed to keep them organized and save space.

In this tutorial, we learn how to use rmdir command in Linux to remove directories.

rmdir command

rmdir command is used to remove empty directories in Linux and in other operating systems. If the directory contains files and sub directories, then rmdir doesn't remove that directory (ies).

Rmdir is a safe command which helps to avoid accidental loss of data as it won't delete any directory contents or files.

Syntax:

rmdir [option] <directory names>

Without any option, rmdir removes the directory which is specified in the path.

The Linux rmdir command options:

OptionsDescription
-pRemove parent directories
--ignore-fail-on-non-emptyIgnore error output when deleting non empty directory
-v, --verboseVerbose information of the command
--versionDisplay version information and exit
--helpDisplay the help page and exit

How to use rmdir in Linux

To use simply type rmdir followed by directory name (s) to delete the specified directories on the command line. The command will run successfully if the directory is empty.

For example to remove an empty directory named dir1 type:

rmdir dir1
rmdir linux remove an empty directory

The rmdir command fail on non empty directory (ies) and gives an error message saying 'rmdir: failed to remove 'Directory/' : Directory not empty'. You can use the option --ignore-fail-on-non-empty to remove the error message.

You can use rmdir to remove multiple directories which are empty. Type rmdir followed by a space then provide directory names with spaces:

rmdir dir2 dir3 dir4
rmdir delete multiple directories

In the same way, creating parent directories using -p option of mkdir command, you can use rmdir -p to remove the empty parent directories:

rmdir -p fruits/orange/mango/apple
rmdir delete parent directories

By default, for a successful rmdir run it won't display any output. You can use -v,--verbose option to output a diagnostic message for every directory processed.

rmdir -v dir5
verbose output

Difference between rmdir and rm command

The rmdir removes only empty directories whereas rm command removes both empty and non empty directories. To delete empty directories rm command requires an option to pass but with rmdir command you can simply remove it.

There is no command called rmdir -r or rmdir --recursive to action recursively.

Conclusion

In this tutorial, we learned about Linux rmdir command to delete directories and its options. You can get detailed information about rmdir command by checking the man page by typing man rmdir or rmdir --help from the command line.

linux rmdir help page
SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment