Newsletter Subscribe
Enter your email address below and subscribe to our newsletter
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.
Table of Contents
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:
Command | Description |
---|---|
rm -r directory_name | Removes the specified directory and all its contents recursively. Use with caution as it permanently deletes files and subdirectories. |
rm -rf directory_name | Removes 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_name | Removes the specified directory and all its contents recursively, prompting for confirmation before removing each file or subdirectory. |
rm -rI directory_name | Removes the specified directory and all its contents recursively, prompting once for confirmation before removing all files and subdirectories. |
rm -rv directory_name | Removes the specified directory and all its contents recursively, displaying the name of each file or subdirectory being removed. |
rm -d directory_name | Removes 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_name | Removes the specified empty directory, even if the directory name begins with a hyphen (-). |
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.
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
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:
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
The above image suggests that the directory was removed from the home directory itself.
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:
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/
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:
As you can see, it notifies you every step it makes while removing files and 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/
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:
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
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:
Now, I want to preserve the example
beach while removing the sub-directory
so I’ll use the following command:
rm -r ~/example/*
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.