Linux rmdir Command Examples for Beginners

As Linux users, we interact with the files and directories on a regular basis. One common operation users perform is removing directories from the file system. However, we have to be extra careful while removing the directories. Because carelessly performed removal operations can result in data loss.

In this beginner-friendly article, we will learn about the rmdir command. We will also discuss some of the practical examples that can be used on a day-to-day basis.

The syntax of the rmdir command is similar to other Linux commands. At a high level, it is divided into two parts – options and arguments:

$ rmdir [OPTIONS] ... <DIRECTORY1> <DIRECTORY2> ...

Here, the square brackets ([]) represent the optional arguments whereas angular brackets (<>) represent the mandatory arguments.

Basic Usage of rmdir Command in Linux

As the name suggests, the rmdir command is used to remove the directory. However, it is important to note that it can remove empty directories only. In this section, we will see the basic usage of the rmdir command.

Delete an Empty Directory in Linux

First, create a few empty directories:

$ mkdir dir1 dir2 dir3 dir4

Let’s verify that the required directories have been created:

$ ls -l

Now, let’s remove the dir1 directory and verify that it has been removed:

$ rmdir dir1
$ ls -l

In a similar fashion, we can use the rmdir command to remove multiple empty directories at once.

Let’s remove the remaining directories:

$ rmdir dir2 dir3 dir4

Finally, verify that all the directories have been removed:

$ ls -l

Here, we can see that the ls command doesn’t show any directory.

Linux rmdir Command Usage
Linux rmdir Command Usage

rmdir Verbose Mode

In the previous section, we used the ls command to verify the directory removal. However, it doesn’t make sense to execute one more command just to verify the actions of the previous commands.

In such cases, we can enable the verbose mode using the -v option, which provides diagnostics for every processed directory.

Let’s create the same directory structure that we created previously:

$ mkdir dir1 dir2 dir3 dir4

Now, let’s remove the directories with the verbose mode enabled:

$ rmdir -v dir1 dir2 dir3 dir4
$ ls -l

From the above output, we can conclude that all the directories have been removed.

rmdir Command Verbose Mode
rmdir Command Verbose Mode

Remove Empty Sub-Directories in Linux

We often create sub-directories on a file system, which allows us to organize our data in a proper way. Let’s see how to work with empty sub-directories.

As discussed in the first example, we can remove multiple directories using the rmdir command. However, the situation becomes tricky when sub-directories are large in numbers.

In such cases, we can use the -p option, which removes the directory and all its ancestors. Let’s understand this with an example.

First, create a sub-directory structure:

$ mkdir -p dir1/dir2/dir3/dir4/dir5

In this example, we have used the -p option with the mkdir command to create a sub-directory structure.

Let’s remove all these directories in one go:

$ rmdir -p -v dir1/dir2/dir3/dir4/dir5

rmdir: removing directory, 'dir1/dir2/dir3/dir4/dir5'
rmdir: removing directory, 'dir1/dir2/dir3/dir4'
rmdir: removing directory, 'dir1/dir2/dir3'
rmdir: removing directory, 'dir1/dir2'
rmdir: removing directory, 'dir1'

Here, the verbose mode removes the dir5 directory and all its ancestor directories.

Remove Empty Sub Directories in Linux
Remove Empty Sub Directories in Linux

Handle Directory Not Empty Failure

We already know that the rmdir can remove only empty directories. Any attempt to remove a non-empty directory will result in an error. Though this provides protection against data loss, in some rare cases it can create an issue.

For example, if we try to remove a non-empty directory from the script that is getting executed by Jenkins then the job will report a failure.

To simulate this, let’s try to remove the non-empty directory:

$ mkdir -p dir1/dir2/dir3/dir4/dir5
$ rmdir dir1

rmdir: failed to remove 'dir1': Directory not empty

For such error cases, we can use the --ignore-fail-on-non-empty option, which ignores all the failures that happened due to a non-empty directory.

Let’s use this option with the command and check the return value:

$ rmdir --ignore-fail-on-non-empty dir1
$ echo $?

0

In this example, we can see that the command didn’t report any error and the zero return value indicates the successful command execution. However, it is important to note that this option just suppresses the error and doesn’t remove the non-empty directory.

rmdir - Directory not empty Error
rmdir – Directory not empty Error

Use Regular Expressions in rmdir Command

Similar to other Linux commands, we can use regular expressions with the rmdir command. Let’s see the usage of the following two regular expressions:

  • ? – It matches exactly one character.
  • * – It matches zero or more occurrences of the previous characters.

First, create a few empty directories:

$ mkdir dir1 dir2 dir-01 dir-02

Now, let’s use the '?' regular expression with the string ‘dir’ to remove the dir1 and dir2 directories:

$ rmdir -v dir?

rmdir: removing directory, 'dir1'
rmdir: removing directory, 'dir2'

Here, we can see that the command removed the correct directories.

Next, use the '*' regular expression to remove the other two directories:

$ rmdir -v dir-*

rmdir: removing directory, 'dir-01'
rmdir: removing directory, 'dir-02'

In this example, we can see that the other two directories have been removed.

Use Regular Expressions in rmdir Command
Use Regular Expressions in rmdir Command

In this section, we discussed the usage of only two regular expressions. However, we can also use the other advanced regular expressions with the rmdir command.

In this article, first, we saw the basic usage of the rmdir command. Then we discussed the verbose mode and removal of the sub-directories. Next, we saw how to handle failures when a directory is not empty. Finally, we discussed how to use regular expressions.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Narendra K
I'm an experienced and passionate software engineer with in-depth experience in Linux, Distributed systems, DevOps, and Cloud. My expertise lies in back-end web development, and the main languages in my tech stack are Java, Spring, Python, and Go. I’m a lifelong learner and an open-source enthusiast.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

3 Comments

Leave a Reply
  1. I am not quite a beginner at Linux, but I am very happy to follow this series of articles, with all the options I wasn’t aware of.

    I will always use the -v option with mkdir and rmdir. However when it’s time to remove directories, I always use rm -r.

    Reply
  2. @dragonmouth

    You are right, but we are talking about the rmdir command which never deletes a directory containing files.

    With respect to the system wipe out, this is not true, the rmdir command never wipes out the system if you mistype the command or argument. The rmdir command is used simply to remove empty directories.

    I totally understand, but then it is better to stick with Windows if a user no more wanted to switch to CLI.

    Reply
  3. For beginners, I would recommend using the GUI File Manager that came with their distro. Display the file system, right-click on the directory you want to delete, choose “DELETE” from the menu and you’re done.

    Yes, the command line is efficient and allows for better granularity BUT for a newbie, one mistyped letter can lead to a wiped-out system.

    With most of today’s distros, it is possible to go months without having to resort to CLI. If we want Linux to be used by anybody other than techies, we need to emphasize the GUI tools. After all, Windows and OS/X are GUI-based. You are not going to get very many users to switch just because Linux has a great CLI.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.