In this article, we would discuss how to remove a directory in Ubuntu distribution. We can delete a directory in Ubuntu through Graphical User Interface and Command-line Interface. Although deleting a directory is pretty straight-forward when done through Graphical User Interface. However, things can become a bit tricky when it comes to Command-line Interface.
Word of Caution – Deleting a directory through Graphical User Interface may or may not delete the object permanently. It largely depends on user settings wherein we have the option to send the deleted directory to trash. On the contrary, when we delete it through Command-line interface – it deletes the object instantly. That is, it doesn’t go to Trash folder.
Also, make sure you understand what operations you are trying to execute. If you don’t understand what you are up to then it is best to contact your System Administrator for assistance.
Remove a directory in Ubuntu distribution
We can remove a directory using rmdir and rm command-line utilities. We will discuss each of these in detail with the help of relevant examples next –
I. rmdir command –
rmdir command-line utility can be used to delete empty directories. For instance, we have an empty directory – ABC. So, to delete the empty directory issue the following in terminal –
To remove multiple empty directories then –
rmdir /path/to/ABC /path/to/ABC_1 /path/to/ABC_2 ... /path/to/ABC_n/
To remove multiple nested empty directories –
rmdir -p /path/to/ABC /path/to/ABC_1 /path/to/ABC_2 ... /path/to/ABC_n/
-p option (i.e. parents) remove an empty directory along-with its ancestors.
If ABC directory contains some other objects, then we would get the error –
rmdir: failed to remove 'ABC': Directory not empty
Under such a scenario, we would have to enter the directory and then delete every object inside it. But, this would itself be a tedious task when we already know the whole directory and its contents are no longer needed.
This issue can be resolved through rm command-line utility which is discussed next.
II. rm command –
The rm command-line utility is used to delete both files as well as directories. In addition to, rm command-line utility deletes both empty as well as non-empty directories too.
To remove a non-empty directory, lets say ABC, issue the following in terminal –
rm -rf /path/to/ABC/
-r option –> to delete the directory recursively
-f option –> force, which means it ignores non-existent objects
Also, to remove multiple empty/non-empty directories –
rm -rf /path/to/ABC/ /path/to/ABC_1/ ... /path/to/ABC_n/
In conclusion, we have discussed how to remove a directory in Ubuntu distribution through rmdir and rm command-line utilities.