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.

rmdir Syntax:

Terminal window
rmdir [option] [directory]

rmdir Options:

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.

Parameters:

ParameterDescription
directorySpecify the directory to be removed.

rmdir Command Usage Examples:

Remove an Empty Directory

Terminal window
rmdir directory_name

Removes the specified empty directory named “directory_name”.

Remove Multiple Empty Directories

Terminal window
rmdir dir1 dir2 dir3

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

Remove Directory and Its Contents

Terminal window
rmdir -r myfolder

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

Remove Directory Silently

Terminal window
rmdir -q mydirectory

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

Remove Directory with a Prompt

Terminal window
rmdir -i directory_name

Prompts for confirmation before removing the specified directory.

How do I use rmdir in Linux?

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

Terminal window
rmdir directory_name

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?

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

How to make rmdir quiet?

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

Terminal window
rmdir -q directory_name

How to display verbose output with rmdir?

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?

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?

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

Applications of the rmdir command

  • Removing empty directories
  • Deleting multiple empty directories at once
  • For use in scripts to automate directory removal operations