There are many ways in which you can remove a directory in Linux, here we'll talk about how to do that on a Linux terminal, we all know how easy it is to do it on a GUI system so we won't even mention it.
This tutorial will take you through various ways in which you can delete a directory in Linux.
Delete a directory using "rmdir" command
The rmdir command, short for'remove directory', is a command-line tool that is used to delete empty directories. The operation will be successful if and only if the directory is empty. The syntax for deleting a directory is as follows:
$ rmdir dir_name
If for whatever reason the directory contains files then it will fail with error:
rmdir: dir_name: Directory not empty
Now, there is another way to delete folders and is with the classic "rm" command, it is typically used to delete files, etc, but we can add some arguments to also get rid of folders:
$ rm -r dir_name
This should take care of deleting the folder, it will ask you to confirm and proceed, if it contains files it will also prompt you to confirm deleting those prior to deleting the folder. There is a way to avoid all that confirmation by using the -f option (force):
$ rmdir -rf dir_name
Now, careful as this assumes you know what you're doing, if you enter this command and delete a folder you did not mean to it will be too late (unless you have backups, which you should!)
If you wish to delete multiple directories at once you can use the following command:
$ rmdir -rf dir_name1 dir_name2 dir_name3
That's it, you now know how to delete directories in Linux, please note that the rm command can also be used for regular files (and -rf applies there too).
If you're looking for a Cloud VPS this is the right place, please check our plans at https://owned-networks.net/kvm.html available at 8 Global Locations (In US and Europe)