How to Delete Directory in Linux

How to Delete Directory in Linux
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.

Deleting Directories from the Command Line:

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:

$ rmdir test

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.

$ tree configs/

Now, to remove the directory configs/ run the rm command as follows:

$ rm -rv configs/
Or
$ rm -rfv configs/

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

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:

$ tree path_to_the_directory

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:

$ sudo yum install -y tree

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:

$ sudo apt install -y tree

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:

$ rm -ri images/

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.

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.

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.

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *