Categories: TutorialsUbuntu

How to Delete a Directory in Ubuntu 20.04

Ubuntu, which is based on Debian’s design which is one of the best Linux distro currently available. Although this Linux distro was designed for personal computers (PC), it can also be deployed on servers. Every operating system has different types of directories that store multiple files. Storage space is a common problem nowadays in most systems, so to free up space, or to remove duplicate copies of the same directories we need to delete the directories. Ubuntu provides multiple ways to delete directories to free-up space which will be discussed in detail in this article.

A directory is a categorization structure in the file systems that contain referrals to other computer files and maybe other directories. A directory service’s primary task is to control the interactions between consumers and their IT resources within an organization or manage the same type of data in one place. In this Article deletion process of the directory on the Ubuntu system will be discussed.

Delete a Directory in Ubuntu

Ubuntu provides both ways to delete the directory: Graphical User Interface and Command Line interface. Below mentioned are some ways to delete a directory.

  • Delete Directory using GUI
  • Delete Directory using the rm command
  • Delete Directory using the rmdir command

Below these ways are explained in detail.

Delete Directory using GUI

If you are a beginner and don’t know how to use the Linux terminal, then you can also delete the directory using the GUI method.

To delete the directory, left click on the directory and click on the “move to trash” option. As I wanted to delete the “Linux” directory from the Documents folder, I will move the cursor on it and then left click on it and then click on the option “move to trash” from the drop-down list as shown below.

After selecting this option you should be able to see that the directory is no longer available as shown below.

Delete Directory using rm command

The rm command is abbreviated as “remove”. This command is used to delete things from file systems like Linux like files, symbolized links, directories, and so on. This operation usually works quietly, and you should take caution while using it because after you delete files, you won’t be able to recover the data of those files or directories. It does not delete directory by default unless used with some option

How to Delete Empty Directory

The below-mentioned syntax is used to delete an empty directory using rm.

$ rm -d directory_name

Run the mentioned command below to delete the directory “Linux”.

$ rm -d linux

Output:

The Linux directory will be removed in the below output:

How to Delete Non-Empty Directory

Below mentioned is the syntax to delete all files and directories that aren’t empty using rm.

$ rm -r directory_name

-r: recursive option

Run the command mentioned below to delete the non-empty “Linux” directory.

$ rm -r linux 

Output:

The Linux directory containing files will be removed in the below output:

Below rm is used with the -r and -f arguments to remove non-empty directory and all files without being prompted:

$ rm -rf directory_name

The mentioned command will delete a non-empty “Linux” directory without being prompted.

$ rm -rf linux

Output:

The Linux directory will be removed in the below output:

How to Delete Multiple Directories

Below mentioned is the syntax to delete multiple directories using the rm command.

$ rm -r directory_name1 directory_name2 …..

Run the mentioned below command to delete the “linux1” and “linux2” directory.

$ rm -r linux1 linux2

Output:

The linux1 and linux2 directory will be removed in the below output:

Delete Directory Using the rmdir command

In Linux, the rmdir command is used to delete empty directories or directories from the system. Only if all of the directories specified in the command-line interface are empty, does the rmdir command erase them. If the supplied directory contains any directories or files, the rmdir command will not be able to delete them.

Below mentioned is the syntax to remove an empty directory using rmdir.

$ rmdir directory_name

Run the below-mentioned command to remove the empty “Linux” directory using rmdir.

$ rmdir -r linux

Output:

The Linux directory will be removed in the below output:

Conclusion

To solve the problem of storage space, we need to delete the directories which are no longer in our use. Ubuntu provides multiple ways to delete the directory. This article is about deleting a directory using the GUI method, “rm” command, and “rmdir” command. All these methods are discussed in detail.

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.

Ubuntu Server Admin

Recent Posts

Install LibreOffice Office Suite in Ubuntu 24.04

This is a beginner’s guide shows how to install (or Remove) the popular LibreOffice Office…

2 hours ago

Canonical at Dell Technologies World 2024

Canonical, the company behind Ubuntu and the trusted source for open source software, is thrilled…

20 hours ago

Deploy Kubernetes Cluster on Ubuntu VPS

This article provides a guide for how to deploy a Kubernetes cluster on Ubuntu VPS.…

1 day ago

Enable Automatic Login in Ubuntu 24.04 & Flavors

This simple tutorial shows how to automatically login a user account in Ubuntu 24.04 and…

2 days ago

Ubuntu Pro for EKS is now generally available

May 14, 2024 – Canonical, the publisher of Ubuntu, is delighted to announce the general…

3 days ago

Deploy an on-premise data hub with Canonical MAAS, Spark, Kubernetes and Ceph

Download the Spark reference architecture guide In this post we’ll explore deploying a fully operational,…

3 days ago