Linux rmdir Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux rmdir command to remove empty directories and understand the limitations of the command when dealing with non-empty directories. The lab covers the basic syntax of the rmdir command, examples of removing empty directories, and the steps to remove non-empty directories using alternative methods. This lab is part of the "Basic File and Directory Operations" skill set and provides practical knowledge for managing directories in the Linux file system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") subgraph Lab Skills linux/mkdir -.-> lab-422893{{"`Linux rmdir Command with Practical Examples`"}} linux/rm -.-> lab-422893{{"`Linux rmdir Command with Practical Examples`"}} end

Understand the rmdir Command

In this step, you will learn about the rmdir command in Linux, which is used to remove empty directories. The rmdir command is a powerful tool for managing directories in the file system.

The basic syntax for the rmdir command is:

rmdir [options] directory

Here, [options] are the optional flags you can use with the rmdir command, and directory is the name of the directory you want to remove.

Some common options for the rmdir command include:

  • -p: Remove the directory and its parent directories if they are empty.
  • -v: Display a message for each directory removed.

Let's try some examples to understand the rmdir command better.

Example 1: Remove an empty directory

mkdir empty_dir
rmdir empty_dir
Example output:

Example 2: Try to remove a non-empty directory

mkdir non_empty_dir
touch non_empty_dir/file.txt
rmdir non_empty_dir
Example output:
rmdir: failed to remove 'non_empty_dir': Directory not empty

As you can see, the rmdir command cannot remove a non-empty directory. In this case, you need to either remove the files inside the directory first or use the rm -r command to recursively remove the directory and its contents.

Create and Delete Empty Directories

In this step, you will learn how to create and delete empty directories using the mkdir and rmdir commands.

First, let's create some empty directories:

mkdir dir1
mkdir dir2
mkdir dir3

Example output:

labex@ubuntu:~/project$ mkdir dir1
labex@ubuntu:~/project$ mkdir dir2
labex@ubuntu:~/project$ mkdir dir3

Now, let's delete the empty directories using the rmdir command:

rmdir dir1
rmdir dir2
rmdir dir3

Example output:

labex@ubuntu:~/project$ rmdir dir1
labex@ubuntu:~/project$ rmdir dir2
labex@ubuntu:~/project$ rmdir dir3

As you can see, the rmdir command successfully removed the empty directories.

Remove Non-Empty Directories with rmdir

In the previous step, you learned how to remove empty directories using the rmdir command. However, what if the directory you want to remove is not empty? In this step, you will learn how to remove non-empty directories.

First, let's create a non-empty directory:

mkdir non_empty_dir
touch non_empty_dir/file1.txt
touch non_empty_dir/file2.txt

Example output:

labex@ubuntu:~/project$ mkdir non_empty_dir
labex@ubuntu:~/project$ touch non_empty_dir/file1.txt
labex@ubuntu:~/project$ touch non_empty_dir/file2.txt

Now, let's try to remove the non-empty directory using the rmdir command:

rmdir non_empty_dir

Example output:

labex@ubuntu:~/project$ rmdir non_empty_dir
rmdir: failed to remove 'non_empty_dir': Directory not empty

As you can see, the rmdir command fails to remove the non-empty directory. To remove a non-empty directory, you can use the rm -r command, which will recursively remove the directory and its contents:

rm -r non_empty_dir

Example output:

labex@ubuntu:~/project$ rm -r non_empty_dir

The rm -r command successfully removed the non-empty directory and its contents.

Summary

In this lab, you learned about the rmdir command in Linux, which is used to remove empty directories. You explored the basic syntax and common options of the rmdir command, and practiced creating and deleting empty directories. Additionally, you learned that the rmdir command cannot remove non-empty directories, and in such cases, you need to either remove the files inside the directory first or use the rm -r command to recursively remove the directory and its contents.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like