Skip to content

What is rmdir Linux command?

The rmdir command in Linux is used to remove empty directories. It is a simple and efficient way to clean up your file system and remove unwanted directories quickly.

Terminal window
rmdir [option] [directory]
OptionDescription
-pRemove DIRECTORY and its ancestors; e.g., ‘rmdir -p a/b/c’ is similar to ‘rmdir a/b/c a/b a’.
-vOutput a diagnostic for every directory processed.
—ignore-fail-on-non-emptyIgnore each failure to remove a directory that is non-empty.
ParameterDescription
directorySpecify the directory to be removed.
Terminal window
rmdir directory_name

Removes the specified empty directory named “directory_name”.

Terminal window
rmdir dir1 dir2 dir3

Removes multiple empty directories named “dir1”, “dir2”, and “dir3”.

Terminal window
rmdir -r myfolder

Recursively removes the directory “myfolder” along with all its contents.

Terminal window
rmdir -q mydirectory

Removes the directory “mydirectory” without displaying any messages or errors.

Terminal window
rmdir -i directory_name

Prompts for confirmation before removing the specified directory.

To use the rmdir command in bash, execute the following command:

Terminal window
rmdir directory_name

How to remove multiple directories with rmdir?

Section titled “How to remove multiple directories with rmdir?”

To remove multiple directories using rmdir, you can specify the directories as space-separated values in a single command:

Terminal window
rmdir directory1 directory2 directory3

How to force rmdir to remove non-empty directories?

Section titled “How to force rmdir to remove non-empty directories?”

To force the rmdir command to remove non-empty directories, you can use the -p flag, which removes the directory and its parent directories if they become empty.

Terminal window
rmdir -p directory_name

To make the rmdir command quiet and suppress error messages, you can use the -q flag.

Terminal window
rmdir -q directory_name

To display verbose output while using rmdir, you can use the -v flag, which shows the directories being processed.

Terminal window
rmdir -v directory_name

How to remove a directory with a space in its name using rmdir?

Section titled “How to remove a directory with a space in its name using rmdir?”

When removing a directory with a space in its name using the rmdir command, enclose the directory name in quotes to prevent syntax errors.

Terminal window
rmdir "directory with space"

How to interactively remove directories with rmdir?

Section titled “How to interactively remove directories with rmdir?”

To interactively remove directories using rmdir and be prompted for confirmation before deletion, you can use the -i flag.

Terminal window
rmdir -i directory_name
  • Removing empty directories
  • Deleting multiple empty directories at once
  • For use in scripts to automate directory removal operations