How to Delete Directory in Linux

Chưa phân loại
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/
$ 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
$ 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


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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Top 5 Tools for Taking and Editing Screenshots on Linux

Many times you feel the need to capture the screen or a part of it to show someone or to keep for yourself. While on Android,...

Best Command-Line Games for Linux

The command-line isn’t just your biggest ally when using Linux—it can also be the source of entertainment because you...
Chưa phân loại, vps giá rẻ

How to install RawTherapee 5 on Ubuntu, Linux Mint

RawTherapee 5 the most recent release, is a powerful free cross-platform raw photo processing program and written in C++...