![](/uploads/1/2/5/7/125745981/281289691.jpeg)
You can delete directories from the command line in Linux very easily. If you have any graphical desktop environment installed on your computer, you can also delete directories using the graphical user interface. In this article, I am going to show you both ways of deleting directories in Linux. So, let’s get started.
Linux provides different commands in order to remove or delete directories and files. But in some cases, this may not work as expected and we can get a message like rmdir: 'dir' Directory no empty which simply means when we try to delete a directory with rmdir command it is not completed. In this tutorial, we will learn how to remove and delete an empty or full directory.
If a directory you’re trying to remove is empty (does not have any files or directories inside), then you can use the rmdir command to remove the directory from the command line.
For example, let’s say, you have an empty directory test/ which you want to remove.
To remove the empty directory test/, run the following command:
As you can see, the directory test/ is removed.
If the directory you’re trying to remove has contents, you can’t remove it with the rmdir command. You have to use the rm command instead.
Let’s say, you want to remove a directory configs/ which has files and other directories inside.
Now, to remove the directory configs/ run the rm command as follows:
The directory and all the contents (files and directory) of the directory should be removed as you can see in the screenshot below.
If you want, you can remove multiple directories (and its contents) with the rm command as follows:
$ rm-rv directory1 directory2 /path/to/directory3
Or
$ rm-rfv directory1 directory2 /path/to/directory3
Or
$ rm-rfv directory1 directory2 /path/to/directory3
NOTE: The -f option removes the directory and contents without any safety check. So, be sure that you don’t have anything important in the directory that you’re trying to remove. For more information on that, read the next section of this article.
Safety Check Before Removing Directories from the Command Line:
A directory contains a lot of files and other directories. You may not know whether you have any important files in there all the time before you actually removed it (oh no!). So, it’s always a good idea to use programs such as tree to verify that you don’t have anything important in the directory that you want to remove.
The tree program is very simple to use. The format of the tree command is:
The tree command is not available in most of the Linux distribution by default. But you can easily install it from the official package repository of your desired Linux distribution.
CentOS 7 and RHEL 7:
You can install tree package from the official package repository of CentOS 7 or RHEL 7 using the YUM package manager as follows:
Ubuntu/Debian:
On Ubuntu, Debian or any derivatives of Debian, you can install tree package from the official package repository using the APT package manager as follows:
The rm command also has a safety check option. You can use the -i option to tell rm to prompt for confirmation before it removes any file or directory.
To safely remove a directory images/ using rm, run the following command:
Before rm descend (go into) any directory, it will prompt for confirmation. To confirm, press y and then press <Enter>.
rm will also prompt for configuration before it removes any file. To confirm, press y and then press <Enter>.
It will only remove the directory if you confirm removal of everything. Otherwise, it will leave all the files that you don’t want to remove along with the directories. Now, you have a way to move the important files and remove the directory afterward. It’s better than regretting later.
![Linux Linux](/uploads/1/2/5/7/125745981/966137055.jpg)
The -i option will be enabled by default in some Linux distribution. To override it and force rm to remove everything without prompting first, use the -f option.
Removing Directories using Graphical User Interface:
If you’re using graphical desktop environment, then you can use the File Manager (i.e Nautilus, Dolphin etc) included in the desktop environment to remove directories.
![Linux delete file Linux delete file](/uploads/1/2/5/7/125745981/881668836.png)
To remove a directory or directories, select the directory or directories that you want to remove and then press <Shift> + <Delete>. Your file manager should prompt you to confirm the delete operation. To confirm, click on Delete as marked in the screenshot bleow. The directory or directories that you’ve selected should be removed.
So, that’s how you delete a directory or directories in Linux. Thanks for reading this article.
There are a couple of ways to go about deleting a directory in Linux. You can either handle it graphically from your desktop environment, or you can do it directly from the command line. Either way will work, and they’re both just as effective.
Graphical method to delete a directory in Linux
Every desktop environment (or file manager) is slightly different. This article will follow KDE Plasma, but the process is roughly the same with every desktop.
Open your file manager and browse to the location of the directory you want to delete. Once you’ve found it, right click on that directory to open the menu of available actions. Depending on your environment, you may see both “Delete” and “Move to Trash” or just “Move to Trash.” It’s always a safer bet to move the directory to the trash because you can recover it if you make a mistake. Click “Move to Trash” on your menu.
The file manager will ask you if you really want to delete the folder. Confirm that you do. The directory will move from its current location and will appear in the trash folder instead.
The Trash
If you’re absolutely sure you don’t need the directory you just removed, open the trash. You’ll see the folder there, and you can even browse its contents. If there’s nothing there that you need, you can either right click on the folder itself to delete it permanently, or you can click in the white space of the directory to bring up a menu with the option to empty the trash. Do whichever you prefer. Remember, there’s no way to undo this. The directory isn’t coming back.
Using the Command Line to delete a directory in Linux
There’s an even more direct way to remove a directory from the command line. The
rm
command can be used to remove both files and directories.Open a terminal window and navigate to the location of your directory. When you’re there, run the following command:
It didn’t work, did it? That’s because
rm
needs to be given the -r
flag to remove a file recursively to delete a directory. On some systems just using the “-r” flag will prompt a message asking if you’re sure you want to delete the directory. To force it and automatically confirm that you do, use -rf
. Be forewarned, rm
doesn’t move a file to the trash. It simply deletes it entirely. Always double-check when you’re deleting a directory (or files). Really bad things can happen.Alternatively, you can also use the command
rmdir
to delete a directory, but it will only work on an empty directory. So if the folder is full of files, this command will fail. Using rm
is a safer bet.Conclusion
There you have it, two ways to delete directories on a Linux system. Take caution when using either one, and remember, using
rm
, especially as root, can be extremely dangerous.Ebooks
![](/uploads/1/2/5/7/125745981/281289691.jpeg)