There can be various ways to remove a directory in any Linux Operating system. For example, if you are using a GUI of any Linux Operating system like GNOME or KDE, you can remove any directory by just right-clicking on the directory. But, when you are working with a stand-alone Linux server and only have a terminal to work with the Linux Operating system, this guide will help you remove a directory in the Linux Operating system’s terminal.
There are multiple commands for removing a directory in Linux, based on the need and different purposes. This post will let you know about all these commands and perform all of them on Ubuntu 20.04 LTS system. The commands that we are going learn and are usually used to remove a directory in Linux are
So, let’s dive in and learn how to remove or delete a directory in Linux and the different ways to use the above-mentioned commands.
Remove a directory using the ‘rmdir’ command
This rmdir command-line tool is specifically used for removing or deleting a directory in the Linux Operating system.
The syntax of using the rmdir command is pretty simple and easy. Just provide the directory name after the rmdir command, and the directory will be removed only if it is empty.
If the directory is empty, it will be removed immediately. Otherwise, it will prompt an error something like “failed to remove ‘directory_name’: Directory not empty”. This message ultimately means that the directory includes some files or subdirectories.
So, if you want to remove the directory and everything in it, you need to switch to the rm command.
Remove a directory using the ‘rm’ command
The rm command does not only used to remove or delete directories, but it is also used to delete both directories and files. It also comes with some additional flags which help in performing different tasks according to the requirement.
For example, to remove a directory and its subdirectories, the -r flag is used to remove a directory, its subdirectories, and files recursively.
This command will remove the directory and all the subdirectories and files in it immediately, only if the directory is not write-protected.
If the directory is write-protected, it will prompt for confirming the deletion process of a write-protected file. If you do not want to get prompted for the confirmation, you need to use the -f option for deleting a write-protected directory in Linux forcefully.
So, the command for deleting a write-protected directory and all the files and subdirectories in it will go like this:
Using the rm command, you can remove multiple directories in a single command as well. The command for deleting multiple directories would be like this:
This is how simply you can remove multiple directories in a single command using the rm command.
This post comprises detailed guidance on removing a directory in Linux using the rmdir and rm command. We have learned about various flags like a r flag that can recursively delete files and subdirectories. The -f flag can be used to delete a write-protected directory forcefully.