How to Delete a Directory in Linux

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.

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)

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

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 everyLinux Distribution.

Delete Directory Using thermdirCommand

Thermdircommand 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 <directory_name>

Some of the options that thermdircommand can take are:OptionDescription–ignore-fail-on-non-emptyused to delete non-empty directories-p, –parentsused to delete the directory along with its children specified-v, –verboseused to get a diagnostic message for every directory

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.

When you will try to delete a non-empty directory using thermdircommand, 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 thermCommand

Thermcommand 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 <file_name/directory_name>

Some of the options which the command can take are:OptionDescription-fWhen this flag is used, the confirmation prompt will not appear, and all the nonexistent files and directories will be ignored-iWhen this flag is used, the command will ask for confirmation from the user for each deletion.-rWhen this flag is used, the command will remove all the contents of the directory specified.-dThis flag is used to remove empty directories.-vThis flag is used to get an explanation of what is being done currently.

To delete empty directories in Linux, use the-dflag with the command as shown below:

rm -d <directory_name>

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-rflag with the command as shown below:

rm -r <directory_name>

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-fflag as shown below:

rm -rf <directory_name>

This command can prove to be quite disastrous if executed unintentionally in the root directory.

When deleting multiple files, use the-iflag with thermcommand to get a prompt before each file as shown below:

rm -ri <directory_name>

Delete Directories Using thefindCommand

You can also delete folders using thefindcommand with the-deleteflag 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.

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, thefindcommand looks for directories matching with the<directory_name>in the<path_to_search>and then the-execflag will pass the searched items to thermcommand, which will delete the directory using the-rflag.

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.

The command line method is the fastest way to delete directories. You can usermdir,rm, andfindcommands 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 <directory_name>

Beebom Staff

Bringing the latest in technology, gaming, and entertainment is our superhero team of staff writers. They have a keen eye for latest stories, happenings, and even memes for tech enthusiasts.

Add new comment

Name

Email ID

Δ

01

02