rmdir Command in Linux With Examples (2024)

In Linux, managing directories efficiently is a crucial skill for both system administrators and everyday users. The ‘rmdir’ command is a specialized tool designed specifically for removing empty directories, helping to maintain a clean file system and avoid accidental data loss. This guide delves into the usage, options, and practical examples of the ‘rmdir’ command, offering insights into its safe and effective application.

Table of Content

  • What is rmdir Command?
  • Options of rmdir Command
  • Examples of rmdir Command in Linux
  • Difference between rmdir and rm Command
  • Trouble shooting Issues with rmdir Command
  • Best practices for safely removing Directories

What is rmdir Command?

The ‘rmdir' command in Linux is specifically designed for removing empty directories. Unlike the rm command, which can delete both files and directories, rmdir focuses solely on directories. It is a straightforward tool but nonetheless crucial for maintaining a tidy directory structure on your system. It ensures whether the directories are empty or not before deleting it and it will delete only if those are empty. It helps to prevent data loss by accidentally removing the data contained directories.

Syntax:

The basic syntax of the rmdir command is:

rmdir [option]... [directory]...

Here, ‘[directory]…’ refers to one or more directories you wish to remove.

Options of rmdir Command

The following are the options of rmdir command:

OptionDescription
--ignore-fail-on-non-emptyIt prevents the errors if the directory is not empty.
-p or --parentsIt will removes the directory and its parent directories if they are empty.
-v or --verboseIt helps in displaying the message for each directory that is removed.
--helpIt will displays the helpful information and exits.
--versionIt displays version information and exits.

Examples of ‘rmdir’ Command in Linux

‘rmdir’ command is similar to the rm command, but rmdir only removes empty directories. So first, we will use the help flag to list down all the available options for the rmdir command:

rmdir --help

rmdir Command in Linux With Examples (1)

Example 1: The Basic rmdir Command

Let’s start the examples with a section with the simple rmdir command to remove multiple directories, and here is the basic syntax:

rmdir mydir1 mydir2 mydir3 .....
  • Here we will remove LINUX, INFO, and DETAIL directories through the following command:
rmdir LINUX INFO DETAIL

rmdir Command in Linux With Examples (2)

Example 2: The ‘-p’ Option

You can use the -p option with the rmdir command to delete a directory, including all the subdirectories:

rmdir -p mydir1/mydir2/mydir3/...../mydirN
  • For example, we will delete the LINUX directory, including all its all ancestors, through the following command:
rmdir -p LINUX/mydir1/mydir2/mydir3

rmdir Command in Linux With Examples (3)

Example 3: The ‘-v’ Option

If you want the terminal to display the message after removing the directory, you can use the -v option with the rmdir command:

rmdir -v dir1 dir2 dir3

Let’s now delete the LINUX, INFO, and DETAIL directories and display the message after their successful removal:

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

rmdir Command in Linux With Examples (4)

Example 4: Remove Multiple Directories With the Same Expression

You can delete multiple directories if they have the same expressions by using the * in the rmdir command. For example, let’s remove all those directories which contain LINUX in their name:

ls 
LINUX1
LINUX2
LINUX3

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

rmdir Command in Linux With Examples (5)

In the above command, we have used the ls command to list all the available directories. Moreover, we executed the rmdir command ‘-v’ option and * to delete all those directories which contain the same expression.

Example 5: The ‘–ignore-fail-on-non-empty’ Option

Sometimes you get the following error while removing a directory through the rmdir command:

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

So, in this case, you can use the –ignore-fail-on-non-empty to ignore the occurrences due to the non-empty directories. For instance, let’s remove the LINUX directory that contains sub-directories:

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

rmdir Command in Linux With Examples (6)

Difference between ‘rmdir’ and ‘rm’ Command

The following are the difference between rmdir and rm command:

Aspectrmdir Commandrm Command
Primary FunctionIt removes the empty directoriesIt will remove the files and directories
Usage LimitationIt can only remove empty directoriesIt can remove both empty and non-empty entities
Recursive DeletionNot applicable to this commandIt can delete directories and their contents recursively with -r option
SafetyIt is safer, as it only deletes if directories are emptyIt requires caution, especially with -r and -f options
Command ComplexitySimple and straightforwardMore complex with multiple options and flags for different use cases

Trouble shooting Issues with rmdir Command

The following are the some of the trouble shooting issues regarding with rmdir command:

1. Directory Not Empty Error

Issue: rmdir fails with an error if the directory is not empty.

Solution: It ensuring that the directory should be empty before attempting to remove it. You can use rm -r command if you need to delete non-empty directories.

2. Permission Denied

Issue: Insufficient permissions to remove the directory.

Solution: Firstly check and modify the directory permissions or use sudo rmdir for having the necessary administrative privileges.

3. Directory Not Found

Issue: Specified directory does not exist or the path is incorrect.

Solution: Verify whether the directory path that is mentioned is existing or not.

4. Using ‘–ignore-fail-on-non-empty’

Issue: rmdir returns errors when directories are not empty.

Solution: Try to usse the --ignore-fail-on-non-empty option to suppress errors for non-empty directories, although it won’t remove them.

Best practices for safely removing Directories

The following are the some of the best practices for removing the directories safely:

  • Check Directory Contents: Firstly before deleting the directories, verify whether the directory is empty or not because rmdir is only helpful in deleting the empty directories..
  • Use ‘rmdir' for Empty Directories: Try to prefer using rmdir for removing empty directories to minimize the risk of accidentally deleting important files.
  • Use ‘rm -r' with Caution: When using rm -r command, we delete non-empty directories. This command should be used with caution, double-check the command and the directory path to ensure you do not remove critical files.
  • Backup Important Data: Try on always backup important data before performing deletion operations, especially when dealing with directories that may contain valuable information.

Conclusion

This was a brief explanation of the rmdir command in Linux with examples. We have mentioned every option you can try while removing an empty directory from the terminal. If you are a beginner, you may receive errors while removing an empty or a non-empty directory. That’s we have included the explanation on the –ignore-fail-on-non-empty option you can use if you get an error while using the rmdir command. Moreover, you can use multiple options of the rmdir command to remove the directories per the requirements.

rmdir Command in Linux – FAQs

How to remove a directory in Linux?

On using the command like`rmdir <directory_name>` we can remove an empty directory, and we can use `rm -r <directory_name>` to remove a non-empty directory with all its contents.

What is the rmdir command in Linux?

`rmdir` is a command in Linux that is used for removing empty directories.

How do I delete a full directory in rmdir?

You can’t delete a full directory with `rmdir` as it only removes empty directories, For this situation try to use `rm -r <directory_name>` with caution for deleting a directory with all its contents.

How do I completely empty a directory in Linux?

On using the`rm -r <directory_name>/*` we can recursively delete all files and subdirectories within a directory, leaving the directory structure intact.

How do I delete a non-empty directory?

Use `rm -r <directory_name>` to delete a non-empty directory and all its contents recursively.



R

rahulkumarmandal

rmdir Command in Linux With Examples (7)

Improve

Previous Article

rmmod command in Linux with Examples

Next Article

rsync command in Linux with Examples

rmdir Command in Linux With Examples (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Neely Ledner

Last Updated:

Views: 5989

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.