How to Use rmdir Command in Linux

With the rmdir command in Linux, you can seamlessly delete empty directories. It is a promising approach, especially when you’re working on a headless server or looking to remove multiple directories at once.

Creating directories and subdirectories is a common practice that aims to keep the data organized. But over the period, you might also need to shed some. This is when you’ll need the rmdir or the remove directory command.

Removing files with the rmdir command in Linux is not like the usual file manager strategy. When the latter is used, the directory gets moved to the trash before getting entirely eliminated, while the scenes are different when you decide to employ the former.

On that note, it is very crucial to be extra careful while using rmdir commands for removing files or directories. 

rmdir Command in Linux: Syntax

Before learning the uses of the rmdir command in Linux, let’s have a quick look at its syntax. Here is how the basic structure of the rmdir command looks like:

$ rmdir [OPTION]... [DIRECTORY]


-v Flag: It will print the verbose output.

-p Flag: This option helps remove parent directories.

[DIRECTORY]: It represents the file that you wish to remove. If you’re looking to delete a directory whose name contains spaces, make sure to escape that with a backslash (/), or else the code will fail to read it.

Uses of rmdir Command in Linux

Removing a Directory Using rmdir Command in Linux

The very basic utility of the rmdir command is, of course, removing directories, whether single or multiple ones. So if you’re after getting rid of a single empty directory, run the rmdir command in the following format:

$ rmdir path/directory

For Example:

$ rmdir ~/Documents/newfolder
rmdir command in linux

Here /Documents/newfolder defines the path to the directory named newfolder. Running the code as mentioned above will remove the directory located in the ~/Documents path, but only when the newfolder is empty.

In case the directory newfolder contains some files, running the rmdir command will fail to execute, followed by printing the “failed to remove” message.

rmdir command for non empty directories

Employing rmdir Command in Linux to Remove Multiple Directories

Similar to how the rmdir command in Linux works for single and empty directories, you can instruct the system to use the same and remove multiple directories. For that, launch the Terminal and run the rmdir command in the following manner:

$ rmdir path directory 1 directory 2

For Example:

$ rmdir ~/Documents/ newfolder games songs
rmdir command for multiple directories

Running the code as mentioned above will remove the newfolder directory in Documents alongside games and songs, provided all the directories are empty.

Checking the Verbose Output

Users willing to see the execution of the rmdir command in Linux can simply use the -v flag followed by the directory or path. For instance:

$ rmdir -v ~/Documents/newfolder


verbose output

Using rmdir Command to Remove Directories Together with their Parent Directory

Besides removing single or multiple empty directories, the rmdir command in Linux can also help remove parent directories.  Consider a three-layered directory structure, DocumentsN>newfolder>newfiles

Now, if you wish to remove the newfiles directory alongside DocumentsN and newfolder, pass the -p flag. However, it is crucial to remember that having sudo privileges is essential for successfully performing this action.

$ sudo rmdir -v -p DocumentsN/newfolder/newfiles
rmdir command parent directory

Suppressing the Fail Message: rmdir Command in Linux

The rmdir command works only for empty directories. Running the command for non-empty ones will just print the fail message. However, there is a cool trick to suppress the message from popping up.

Using the --ignore-fail-on-non-empty option will instruct the system not to print the failed message as output. From the name itself, you can understand the purpose of this flag.

ignore failed message

But remember, the rmdir command will still not remove the non-empty directory, whether you use the ignore option or not.

rmdir Command Vs. rm Command in Linux

As already mentioned, the rmdir command in Linux is used to remove empty directories. Employing the rmdir command for directories that contain files will eventually leave an error message. This is when the rm command clicks in.

The rm command is a utility that allows users to remove a directory that may or may not be empty. However, one thing that sits common in both rmdir and rm commands is that running either will remove the directory entirely without storing those in trash.

Using rm Command in Linux

The rm command, when used without options, does nothing. To use the command for deleting empty directories, you’ll need to pass the -d (--dire) option followed by the path, while using -r (--recursive or -R) will instruct the system to delete a non-empty directory.

For example, If you want to remove an empty directory named NewF, use the following command:

$ rm -d (--dir) NewF
using rm command

For deleting a non-empty directory named NewF2, run the following command:

$ rm -r NewF2
rm command for non-empty directories

In a situation when you intend to use the rm command for deleting a write-protected directory, the system will ensure to display a confirmation prompt. Users who don’t want to see the prompt repeatedly can simply use the -f flag.

For example:

$ rm -rf NewF2
rm command no prompt

Deleting Multiple Directories with rm Command

For removing multiple directories at one go, invoke the rm command followed by the directory names. Make sure to include spaces in between.

For example:

$ rm -r Newfile1 Newfile2 Newfile3
removing multiple directories with rm command

Activating Prompt for Confirming the Removal of Directories

Using the -i flag alongside the rm command will make the command-line throw a confirmation prompt each time it intends to delete a subdirectory and file. 

The same can get annoying, especially when the number of files is pretty large. In that case, you can use the -I flag. What it will do is ask for confirmation only once before proceeding.

As a part of GNU Core utilities, the rmdir command in Linux is a powerful tool for removing empty directories. Walking through this article will help you learn using this command in the most efficient manner.

The article also discusses what happens when you try removing a non-empty directory with the rmdir command. To add to that, you’ll get to know the uses of the rm command and how it is different from rmdir.

If this guide helped you, please share it.

Leave a Reply
Related Posts