How to delete directories in Linux using rm and rmdir commands

Key takeaways

  • The rm command + the -r option removes a directory and all its contents while the rmdir command only works on empty directories.
  • Deleting directories with the rm and rmdir commands is irreversible, so always verify you’re targeting the correct directory to avoid accidental data loss.
  • Options like -i with the rm command let you confirm each deletion, and rmdir -p can delete parent directories along with the empty directory.

Directories make up the file management system in your computer’s operating system. Removing unnecessary directories is important because it improves your device’s performance and allows you to organize your data effectively.

In this guide, we’ll show you how to delete a directory in the Linux command line interface using the rm and rmdir commands. We’ll also include their different command options and their functions.

Removing directories in Linux

In Linux, directories serve as containers for files and subdirectories. They create a hierarchical structure, helping system administrators and the system organize and manage data efficiently.

Users need to remove directories from their operating system for various reasons. The most important one is to declutter old data and make space for new files.

To begin deleting directories in Linux, you’ll need to open a terminal window to interact with your operating system. Opening one can vary depending on your desktop environment.

Typically, you can open a terminal window by pressing Ctrl+Alt+T. If that doesn’t work, search for “Terminal” or “Shell” in your application menu instead. A terminal window typically looks like this:

When deleting directories in Linux, you only need to remember the rm and rmdir commands. The removal process is straightforward, but we recommend you proceed cautiously as this action is irreversible. It’s best to double-check the directories you’re targeting to delete.

Are rm and rmdir commands different from each other?

The rm and rmdir commands serve different purposes. The rm command deletes non-empty directories and all their contents, while the rmdir command can only remove empty directories.

While both commands function differently, they’re equally important in managing Linux directories.

How to delete a non-empty directory using the rm command

In this process, you need to be familiar with the different rm command options and their functions. Take a look at the table below before proceeding to the steps in deleting a non-empty directory.

rm Command + OptionFunction
rm -rDeletes the directory and its files and subdirectories
rm -vShows deleted files and subdirectories
rm -iAsks for confirmation before deleting each file and subdirectory
rm -IAsks for confirmation before deleting more than three files and subdirectories at once
rm -dRemoves empty directories

NOTE: For consistency, we’ll label our sample non-empty directory with “myexample.”

Step 1: Navigate to the non-empty directory

In the terminal window, enter the cd command + myexample and press Enter. This takes you to the non-empty directory you intend to delete.

cd myexample

Step 2: Check the contents of the directory

Next, type the ls command and hit Enter. This allows you to see the list of the directory’s contents to confirm you’re deleting the right one.

Step 3: Delete the directory

After checking, type the rm -r command + myexample and press Enter to delete the specified directory and all its contents.

rm -r myexample

If you want to see the list of deleted files and subdirectories, you can use the rm -rv command + myexample.

rm -rv myexample

Use the commands rm -i and rm -I + myexample if you prefer to be asked before deleting files or subdirectories. Use the former if you want to confirm removals one by one or the latter if you prefer to confirm multiple file or subdirectory deletions at once.

Below are the sample confirmation prompts you receive when you type rm -i and rm -I, respectively:

rm: remove ‘[file/subdirectory]’?
rm: remove 5 arguments?

To confirm the removal/s, type “Y” and hit Enter.

As mentioned previously, the rm command can also remove empty directories. To do this, type the rm -d command + [empty directory].

rm -d [empty directory]

Step 4: Confirm the directory removal

Lastly, use the cd command + the parent directory of the non-empty directory. For example, if you removed the “home/user/myexample” directory, enter the syntax below:

cd home/user

Then, type the ls command and press Enter. If you can’t find the “myexample” directory in the list, then the deletion was successful.

How to delete an empty directory using the rmdir command

Like the rm command, the rmdir command has a few command options, but we’ll focus on the two main ones.

rmdir Command + OptionFunction
rmdir -pDeletes the empty directory’s parent
rmdir -vShows a confirmation message verifying the deletion

NOTE: For consistency, we’ll label our sample empty directory with “emptynest.”

Step 1: Go to the empty directory

Type the cd command + emptynest and hit Enter.

cd emptynest

Step 2: Remove the directory

Enter the rmdir command + emptynest to delete the directory.

rmdir emptynest

If you intend to remove the empty directory and its parent, use the rmdir -p command + the directory path. Here’s an example:

rmdir -p user/documents/emptynest

To delete multiple empty directories, enter the rmdir command + the directories.

rmdir emptynest1 emptynest2 emptynest3

Suppose you want to confirm the deletion process, type the rmdir -v command + emptynest. The sample confirmation message will look like this:

rmdir: removing directory, ‘emptynest’

Since the rmdir command can only remove empty directories, it will give you the error message below when it encounters a non-empty directory:

rmdir: failed to remove [non-empty directory]: Directory not empty

Step 3: Verify the directory deletion

Type the cd command + the parent directory of the empty directory. For instance, if you deleted the “user/documents/emptynest” directory, use the syntax below:

cd user/documents

Then, type the ls command and hit Enter. You can confirm that the removal was successful if you can’t find the “emptynest” directory in the list.

Declutter your Linux directories for efficient file management

Deleting non-essential directories in your Linux operating system helps you organize your computer data. Use our guide to maintain your directory structure safely and avoid removing important files permanently. Add to that, always double-check the directories you’re targeting and delete cautiously.

If you prefer safer options, you can always try removing directories using these alternatives: a graphical user interface (GUI) or trash-cli.


Joan Lora
Joan Lora

Joan is a Content Marketing Writer at Domain.com. She aims to create blog articles that inspire brands and businesses to take their online ventures to the next level. Outside of work, you'll find her posting song covers and self-help content on TikTok. She's also into cosmetics and wants to become a sought-after makeup artist one day.

Joan Lora
Joan Lora

Joan is a Content Marketing Writer at Domain.com. She aims to create blog articles that inspire brands and businesses to take their online ventures to the next level. Outside of work, you'll find her posting song covers and self-help content on TikTok. She's also into cosmetics and wants to become a sought-after makeup artist one day.