How To Delete A Directory In Linux In 2024 4 Methods
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:
- First, open any file manager of your choice and navigate to the path where you want to delete the directory.
- 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.
- 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.
- 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
Delete Empty Directory in Linux
To delete an empty directory in Linux, use the following command:
rmdir <directory_name>
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 ‘<directory_name>’: Directory not empty
To delete a non-empty directory in Linux, use the following command:
rmdir –ignore-fail-on-non-empty <directory_name>
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
Delete Empty Directories
To delete empty directories in Linux, use the -d flag with the command as shown below:
rm -d <directory_name>
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 <directory_name>
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 <directory_name>
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 <directory_name>
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 <path_to_search> -type d -name “directory_name” -delete
This command will look for the empty directory specified by the parameter <directory_name> in the given path and delete them.
Delete Non-Empty Directories
To delete non-empty directories using the find command, use the following syntax:
find <path_to_search> -type d -name “directory_name” -exec rm -r {} +
Understanding the above syntax:
In the above syntax, the find command looks for directories matching with the <directory_name> in the <path_to_search> and then the -exec flag will pass the searched items to the rm command, which will delete the directory using the -r flag.
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.