Ubuntu

How to Delete a Directory in Ubuntu 20.04

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.

Graphical user interface, application Description automatically generated

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

Graphical user interface, application, Teams Description automatically generated

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

Text Description automatically generated

Output:

The Linux directory will be removed in the below output:

Text Description automatically generated

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 

Graphical user interface Description automatically generated with medium confidence

Output:

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

Graphical user interface, text Description automatically generated

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

Text Description automatically generated

Output:

The Linux directory will be removed in the below output:

Graphical user interface, text Description automatically generated

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

Text Description automatically generated with medium confidence

Output:

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

Graphical user interface, text Description automatically generated

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

Text Description automatically generated

Output:

The Linux directory will be removed in the below output:

Graphical user interface Description automatically generated

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.

Similar Posts