Enter your email address below and subscribe to our newsletter

How to Remove (Delete) Directory in Linux

Learn how to delete a directory in Linux using the rm command.

Share your love

I believe creating a directory is easy compared to deleting a directory in Linux, as it requires more flags and a better understanding of how things work under the hood.

To remove a directory, you can either use the rm command or the rmdir command. I recommend using the rm command as you are already used to using the rm command to delete files, so it’ll be easily compared to learning something from scratch.

So in this tutorial, I will walk you through how you can use the rm command in Linux to remove directories with practical examples.

Remove the directory in the Linux

As discussed earlier, this tutorial will be focused on how you can use the rm command to remove directories. But to get the most out of the rm command, it is necessary to know its syntax, so I’ll be starting off with the syntax of the rm command.

To use the rm command to delete directories, you need to use the -r flag with the rm command. It is used for the recursive removal which means it will remove every file inside of the given directory and then remove the directory itself.

So here’s a syntax of the rm command with the -r flag that you’ll be using to remove directories:

rm -r<other options> Directoy name(s)

Here,

  • -r: it is a specific option used with the rm command to work with directories and if you skip the -r flag, it will assume that the user wants to remove the file instead of the directory.
  • <other_options>: here’s where you tune the rm command as per your needs to remove a directory. For example, you can use the -i flag to prompt for confirmation before removing each file or directory recursively.
  • Directory name(s): Here you specify the name of one or more directories that you want to remove. You can also specify the path to the directory if those are not present in the current working directory.

Now, let’s take a look at some options that you can use with the rm -r command:

CommandDescription
rm -r directory_nameRemoves the specified directory and all its contents recursively. Use with caution as it permanently deletes files and subdirectories.
rm -rf directory_nameRemoves the specified directory and all its contents recursively, without prompting for confirmation. Use with extreme caution as it permanently deletes files and subdirectories without any confirmation.
rm -ri directory_nameRemoves the specified directory and all its contents recursively, prompting for confirmation before removing each file or subdirectory.
rm -rI directory_nameRemoves the specified directory and all its contents recursively, prompting once for confirmation before removing all files and subdirectories.
rm -rv directory_nameRemoves the specified directory and all its contents recursively, displaying the name of each file or subdirectory being removed.
rm -d directory_nameRemoves the specified empty directory. If the directory is not empty, it will not be removed, and an error message will be displayed.
rm -d -- directory_nameRemoves the specified empty directory, even if the directory name begins with a hyphen (-).

Examples of how to remove a directory in Linux

In this section, I will walk you through multiple examples of how you can remove a directory for different scenarios. Let’s start with a basic one and later move on to some intermediate ones.

1. Remove an empty directory

To remove an empty directory, you can skip using the -r as it does not require the recursive removal of files. Instead, you use the -d flag as shown here:

rm -d <directory_name>

Let’s say I want to remove an empty directory called empty-dir, so I will use the -d flag in the following manner:

rm -d empty-dir
Remove an empty directory in Linux

2. Delete a non-empty directory and its contents

When you have a directory containing multiple files and sub-directories, you use the -r flag for recursive removal of the directory and its contents as shown here:

rm -r ~/directory_name

For example, I want to delete a directory named non-empty which consists of the following files:

Use tree command to show the directory structure

As you can see, it not just only contains multiple images but also contains media files in the subdirectory and to delete each file including the directory itself, here’s how I’ll use the rm command:

rm -r non-empty
Remove directory recursively in Linux using the rm command

The above image suggests that the directory was removed from the home directory itself.

3. Delete write-protected files

When you remove files recursively, you may encounter a prompt asking for your permission if you want to remove a write-protected file or not. For a few times, it’s ok, but when you’re dealing with a server, it becomes a headache:

Prompt to remove write-protected files while removing directories in Linux

To get away with such a problem, you can pair the rm -r command with the -f flag for the forced removal of the files (without a prompt):

rm -rf Directory_name

For example, here, I removed the same directory but with the rm -rf command:

rm -rf example/
Remove write-protected directories recursively in Linux using the rm command

4. Get a detailed output while deleting directories

When you delete directories in Linux recursively, it doesn’t actually tell you anything unless a file is write-protected and prompts you for your permission to remove it.

But sometimes you want to know what is being done right after executing the command. In Linux, it is called getting a verbose output, and it can be triggered using the -v flag:

rm -rv ~/Directory_name

Here’s an example of with and without verbose output while removing the example directory:

Get verbose output while removing directories recursively using the rm command in Linux

As you can see, it notifies you every step it makes while removing files and directories.

5. Prompt for confirmation while removing directories

While removing directories recursively, you may end up removing important subdirectories. Sure, the verbose output helps but only to a certain extent. For better control, you can get a prompt before every removal of a file and directory to decide which one to keep and which one to remove.

To get a prompt before removal of every file and directory, you use the -i flag as shown here:

rm -ri Directory_name

For example, here, I wanted a confirmation prompt before removing the example directory, so I used the following command:

rm -ri example/
Get a confirmation prompt before removing each directory in Linux using the rm command

6. Delete a directory starting with a hyphen (-)

When you have a directory starting with hyphen (-), the regular rm -r command or adding a different option does not work. It simply gives you an error saying invalid operation:

Remove a directory starting with hyphen in linux

To solve this problem, all you have to do is add two hyphens between the flag (-r) and the directory name as shown here:

rm -r -- Directory_name

Here’s how I removed a directory named -example:

rm -r -- example
Remove a directory starting with hyphen in linux

7. Preserve a top-level directory

When removing directories recursively, you may want to remove the top-level of the directory and remove only the subdirectories. In that case, you can preserve a specific directory structure by giving its path and at the end adding an asterisk (*) as shown here:

rm -r ~/directories/to/keep/*

For example, here’s a directory structure I’m working with:

Directory hierarchy in linux

Now, I want to preserve the example beach while removing the sub-directory so I’ll use the following command:

rm -r ~/example/*
Remove directories while keeping the parent branch in linux

Wrapping Up…

In this tutorial, I went through how you can remove directories using the rm command including syntax, common options, and 7 practical examples.

Sure, it can be merged with other commands such as find and exec to create a robust solution but this was intended for beginners so I’ve only included simple ones.

Let me know if you want me to cover advanced examples as well.

Share your love
Kabir
Kabir

A tech journalist whose life revolves around networks.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!