How to Delete Directory in Linux

29/12/2020
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/
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.

Sandclock 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, Sandclock 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

Install AWS Command Line Interface (CLI) on Ubuntu 18.04 LTS

AWS CLI or Amazon Web Service Command Line Interface is a command line tool for managing and administering your Amazon...
29/12/2020

Install Manjaro Linux on VirtualBox

Manjaro Linux is based off of the Arch Linux distribution. It is meant to provide all the benefits of Arch Linux, its modularity,...
29/12/2020

Mounting VMware Shares from the Command Line on Linux VM

In this article, I am going to show you how to share a directory/folder to a VMware Virtual Machine (VM) and how to mount...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019
26/10/2021

How to Update Ubuntu Linux
24/10/2021

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu
20/10/2021

Hướng dẫn cài đặt Apache CloudStack 4.15.2.0
19/10/2021

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader
19/10/2021