May 10, 2024 4 min read

rmdir command in Linux with Examples

In this tutorial, we will explain how to use the rmdir command in Linux, which is used to remove directories or folders from the file system.

rmdir command on Linux
rmdir command on Linux
Table of Contents

Introduction

Before we discuss rmdir command in Linux, let's briefly understand - What is rmdir command ?

The rmdir command on Linux is used to remove directories or folders from the file system. It is a built-in command that helps users delete empty directories quickly and efficiently.

Unlike the rm command, which deletes files, rmdir focuses specifically on removing directories. By using rmdir, users can clean up their directory structure and streamline their file system organization, making it a convenient tool for managing directories with ease.

In this tutorial, you will understand rmdir command in Linux. We will also address a few FAQs on rmdir command in Linux.

Introduction to rmdir Command

In Linux, empty directories are specifically meant to be removed using the rmdir command. rmdir only works with directories, as opposed to the rm command, which can remove both files and directories. It's a simple tool, but it's essential for keeping your system's directory structure organized.

Basic Syntax

The rmdir command's basic syntax is as follows:

rmdir [option] directory_name

The name of the directory you wish to remove is indicated by directory_name in this case.

rmdir Command

Similar to the rm command, the rmdir command only deletes empty directories. Therefore, we will first list all the options available for the rmdir command using the help flag:

rmdir --help

The command above shows the different options, including:

Understanding Options

  • -p, --parents: When the specified directory is removed, this option allows parent directories to be removed as well, in case they become empty. If the parent directories of the directory become empty, it effectively removes both of them.
  • -v, --verbose: When activated, this option gives feedback on the action taken by displaying a message for every directory processed.
  • --ignore-fail-on-non-empty: By default, if the directory is not empty, rmdir fails. This option, however, forces rmdir to carry out the removal despite such errors.
  • --version: This option shows the version details and closes the window.

Exit Status

This command returns the following exit values:

Item Description
0 The operation to remove each directory entry specified by the Directory parameter was completed successfully.
>0 An error occurred.

Example 1: The Basic rmdir Command

To begin the examples, let's create a section using the basic rmdir command to remove multiple directories. The syntax is as follows:

rmdir mydir1 mydir2 mydir3 .....

Here, we'll use the following command to delete the LINUX, INFO, and DETAIL directories:

rmdir LINUX INFO DETAIL

Example 2: The -p Option

The rmdir command can be used with the -p option to remove a directory along with all of its subdirectories:

rmdir -p mydir1/mydir2/mydir3/...../mydirN

For instance, we will use the following command to remove the LINUX directory and all of its ancestors:

rmdir -p LINUX/mydir1/mydir2/mydir3

Example 3: The -v Option

You can use the -v option with the rmdir command to have the message displayed in the terminal after the directory is removed:

rmdir -v dir1 dir2 dir3

Now that the LINUX, INFO, and DETAIL directories have been successfully removed, let's delete them and display the following message:

rmdir -v LINUX INFO DETAIL
rmdir: removing directory, 'LINUX'
rmdir: removing directory, 'INFO'
rmdir: removing directory, 'DETAIL'

Example 4: Remove Multiple Directories With the Same Expression

If several directories have the same expressions, you can delete them all by using the * in the rmdir command. For illustration, let's delete every directory whose name includes LINUX:

ls 
LINUX1
LINUX2
LINUX3

rmdir -v LINUX*
rmdir: removing directory, 'LINUX1'
rmdir: removing directory, 'LINUX2'
rmdir: removing directory, 'LINUX3'

The ls command was used in the command above to list every directory that is available. Additionally, we used the rmdir command with the -v option and * to remove every directory that contained the same expression.

Example 5: The --ignore-fail-on-non-empty Option

When using the rmdir command to remove a directory, you may occasionally encounter the following error:

rmdir <option> <directory> rmdir: failed to remove 'dir1': Directory not empty

To ignore the occurrences caused by the non-empty directories in this situation, use the --ignore-fail-on-non-empty option. Let's take out the LINUX directory, for example, which has the following subdirectories:

rmdir  --ignore-fail-on-non-empty LINUX

FAQs on rmdir command on Linux

Can I delete non-empty directories with rmdir?

No, rmdir can only delete empty directories. To delete directories with contents, you can use the rm -r command.

Is there a way to force delete directories with rmdir?

No, the rmdir command does not have a force delete option. It can only delete empty directories.

How do I delete multiple directories at once with rmdir?

To delete multiple directories simultaneously, you can provide multiple directory names as arguments to the rmdir command.

Can I delete directories with special characters in their names using rmdir?

Yes, you can delete directories with special characters by enclosing the directory name in quotes when executing the rmdir command.

What happens if I try to delete a non-existent directory with rmdir?

If you attempt to delete a directory that doesn't exist, rmdir will return an error message indicating that the directory could not be found.

Does rmdir provide any options to display additional information?

rmdir is a simple command without many options. It mainly focuses on deleting empty directories, so it does not provide additional information.

Are there any potential risks when using the rmdir command?

The main risk when using the rmdir command is accidentally deleting directories that contain important files. It's essential to double-check before executing the command to ensure that you are deleting the intended directories and that they are empty to avoid unintentional data loss.

Conclusion

We hope this tutorial helped you understand how to use rmdir command in Linux.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.