rmdir Command in Linux Explained [With Examples]

Written by: Linuxopsys   |   Last updated: June 30, 2023

The system may always end up with empty directories usually after some projects. The rmdir is the safest command to remove those in Linux.

rmdir command in Linux is used to remove empty directories from the filesystem. By default, rmdir gives an error when it encounters a non-empty directory. Actions only on empty directories. It is simple and straightforward with only a few options.


The basic syntax of rmdir command:

rmdir [options] <directory-name(s)>


rmdir command is simple and straightforward with only a few options. The following table shows rmdir options:

-pRemove parent directories
--ignore-fail-on-non-emptyIgnore error output when deleting a non-empty directory
-v, --verboseVerbose information of the command

Note: You need the necessary permissions to delete a directory. If you're getting a permission error, you may need to use sudo.

How to Use rmdir Command

To delete a single empty directory type rmdir followed by directory name.

For example to remove an empty directory named dir1, type:

rmdir dir1
rmdir linux remove an empty directory

The command will run successfully if the directory is empty.

If the directory is a non-empty directory it gives an error message saying 'rmdir: failed to remove 'Directory/' : Directory not empty'.

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

To remove multiple empty directories, provide directory names as arguments separated with a space. For Example:

rmdir dir2 dir3 dir4
rmdir delete multiple directories

Use -p option with rmdir command to remove a directory and its parent directories. Only works if all directories in the path are empty. For example

rmdir -p dir1/dir2/dir3
example showing rmdir with -p option

In the path, if any directory contains contents obviously the command will fail. This is because rmdir is designed to only remove empty directories. You can add the option `--ignore-fail-on-non-empty to tell rmdir command to proceed with other operations even when it encounters a non-empty directory.

rmdir -p --ignore-fail-on-non-empty dir1/dir2/dir3
example usage of --ignore-fail-on-non-empty option with rmdir command

Remember: This only ignores the alert and parent directories even if empty won't be deleted.

Both rmdir and rm -r are used to remove directories. rmdir only removed empty directories and give an error when it encounters non-empty directories. Whereas rm -r command recursively deletes directories even if it contains contents. Alternatively, you can use rm command with -d option to delete the specified directory if it's empty.


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

Leave a Reply

Leave a Comment