Creating and deleting directories and files on any operating system is one of the most commonly occurring tasks. Depending on your need for storing, retrieving, and removing information and data, you can perform these tasks. However, in Linux, you can achieve almost every task via the command line. Therefore, in this guide, we will learn to delete a directory in Linux Mint 21 through its CLI.
Methods of Deleting a Directory in Linux Mint 21:
Depending on the situation of the directory to be deleted i.e. whether it is empty or not, you can choose any of the following two methods. However, we would first like to show you the two directories that we will be using for these methods. Our first directory is empty and to verify this, we will execute the command shown below:
$ ls -l Dir1
You can confirm from the output of this command shown in the following image that “Dir1” is an empty directory.
Our second directory contains a sub-directory and some files. To verify this, you can use the command shown below:
$ ls -l Dir2
The output of this command shown in the following image depicts the contents of “Dir2”.
Now, according to the state of your directory i.e. Empty or Non-Empty, you can use any of the following two methods.
Method # 1: Deleting an Empty Directory in Linux Mint 21:
For deleting an empty directory in Linux Mint 21, you have to run the command shown below:
$ rmdir Dir1
This command will not produce any output as such.
To confirm if the specified directory has been deleted successfully or not, you can run the “ls” command on it again. This time, this command will not be able to locate the said directory since it has already been deleted. This is shown in the following image:
Method # 2: Deleting a Non-Empty Directory in Linux Mint 21:
Now, for deleting a non-empty directory in Linux Mint 21 along with all of its files and sub-directories, you need to run the command shown below:
$ rm -r Dir2
This command will also not display any output on the terminal.
For confirming if the specified directory has been deleted successfully from your system or not, you can run the “ls” command on it again. Just like the above method, the “ls” command will not be able to locate this directory since it has already been deleted. It is shown in the image that follows:
This article was meant to share with you the methods of deleting a directory in Linux Mint 21. If you have an empty directory to be deleted, then you should opt for the first method. However, if your directory to be deleted is non-empty, then you will have to go with the second method.