Linux is strikingly different from the Windows operating system. For example, if you want to delete a folder in Windows, you can simply right-click on it and delete it. However, things are not that simple in Linux. Deleting a directory or folder in Linux can be accomplished via the graphical user interface as well as the command line interface. If you’re not sure how to delete a directory in Linux, we have prepared a simple yet efficient guide for you. In this article, we will show both the GUI and the CLI methods to delete directories in Linux.
Remove a Directory in Linux (2023)
Folders in Linux are known as directories. And in the world of Linux, everything is treated as a file, even a directory. Let us now see how to delete a directory in Linux using four different ways – one GUI and three CLI.
How to Delete a Directory in Linux (GUI Method)
This method of deleting files is easy and best suited for users who have newly migrated to Linux. It can work on any distribution, provided a “Desktop Environment” and file manager is installed on the computer. In this article, we are using Ubuntu 20.04 LTS with Gnome desktop environment and Nautilus file manager. Here’s how it works:
1. First, open any file manager of your choice and navigate to the path where you want to delete the directory.
2. Select the folder(s) you want to delete and press the “Delete” button on the keyboard. Or, you can also right-click on the selected folder and select “Move to Trash” from the context menu.
3. All deleted files and directories in Linux are not deleted permanently but moved to a special location known as Trash, which is similar to Recycle Bin in Windows.
4. To permanently delete a directory in Linux, select it and press “Shift + Delete” on the keyboard. It will open a prompt whether you want to delete it permanently or not. Click on “Delete” in the dialogue box again.
Delete a Directory in Linux via the Command Line
Doing any task using the command line is faster with many options than the GUI method. Also, the CLI method deletes the files and folders permanently. Here, we will show three commands to delete a directory in Linux that comes preinstalled in every Linux Distribution.
Delete Directory Using the rmdir
Command
The rmdir
command is generally used to delete empty directories, but can also be used to delete non-empty ones. The command does not have a ton of features and options but it gets the job done. The general syntax of the command is as follows:
rmdir
Some of the options that the rmdir
command can take are:
Option | Description |
---|---|
--ignore-fail-on-non-empty |
used to delete non-empty directories |
-p, --parents |
used to delete the directory along with its children specified |
-v, --verbose |
used to get a diagnostic message for every directory |
Delete Empty Directory in Linux
To delete an empty directory in Linux, use the following command:
rmdir
Here, in this example, since we get no output, this means the command got successfully executed and the directory has been deleted.
Delete Non-Empty Directory in Linux
When you will try to delete a non-empty directory using the rmdir
command, you will get the following error:
rmdir: failed to remove '
To delete a non-empty directory in Linux, use the following command:
rmdir --ignore-fail-on-non-empty
Delete Directory Using the rm
Command
The rm
command is a powerful tool that can delete both files as well as directories while providing many great features. The basic syntax of the command is:
rm
Some of the options which the command can take are:
Option | Description |
---|---|
-f |
When this flag is used, the confirmation prompt will not appear, and all the nonexistent files and directories will be ignored |
-i |
When this flag is used, the command will ask for confirmation from the user for each deletion. |
-r |
When this flag is used, the command will remove all the contents of the directory specified. |
-d |
This flag is used to remove empty directories. |
-v |
This flag is used to get an explanation of what is being done currently. |
Delete Empty Directories
To delete empty directories in Linux, use the -d
flag with the command as shown below:
rm -d
Delete Non-Empty Directory
When deleting non-empty contents, it can be very dangerous as some essential files may get deleted. So, be extra cautious when deleting non-empty directories. To delete a directory with all its contents, use the -r
flag with the command as shown below:
rm -r
Force Delete Directories in Linux
The rm command gives a prompt, by default, when deleting the write-protected files and directories. Press either 'y'
or 'n'
depending upon your choice. To bypass the prompt, we use the -f
flag as shown below:
rm -rf
This command can prove to be quite disastrous if executed unintentionally in the root directory.
Prompt Before Deleting Folders in Linux
When deleting multiple files, use the -i
flag with the rm
command to get a prompt before each file as shown below:
rm -ri
Delete Directories Using the find
Command
Delete Empty Directory in Linux
You can also delete folders using the find
command with the -delete
flag as shown below:
find
This command will look for the empty directory specified by the parameter
Delete Non-Empty Directories
To delete non-empty directories using the find command, use the following syntax:
find
+
Understanding the above syntax:
In the above syntax, the find
command looks for directories matching with the in the and then the -exec
flag will pass the searched items to the rm
command, which will delete the directory using the -r
flag.
Frequently Asked Questions
The command line method is the fastest way to delete directories. You can use rmdir
, rm
, and find
commands to delete directories in Linux.
The main reason you cannot remove a directory in Linux is that you do not have appropriate permissions to make any changes in the directory. To delete a directory by bypassing missing permission, use the following command: sudo rm -rf
Delete Files and Directories Using Linux Commands
Deleting directories/files in Linux is a very simple task yet a very important one for users of all kinds. Here, we have shown two ways to delete folders in Linux and we hope this article has given you a good understanding of the two methods and the commands. Do let us know in the comments in case of any issues.