How to delete a directory in Linux (Terminal and GUI)

The directory is a vital part of any Operating System. In this tutorial, I will show you how to delete a directory in Linux using terminal and GUI.

The directories are a vital part of any Operating System. For several reasons sometimes we have to create and delete files in our Linux system. In this tutorial, I will show you how to delete a directory in Linux using the terminal and Graphical User Interface.

If you are using a desktop file manager like Gnom’s Files or KDE Dolphin, you will be able to organize your files using its graphical user interface.

Keep in Mind

When you are going to delete a directory in your Linux system there are several things that you should keep in mind.  the important notice is listed below.

  1. When you select the delete option not (Move to trash) while deleting using desktop file manager your directory will delete forever and you can not be recorded from the trash.
  2. Whenever you are using the terminal to delete your directories you have to be careful because it is a one-way journey.
  3. Make sure your user has sufficient permission to write on the current directory.
  4. If you’re going to delete a directory that contains space inside the name, you must be escaped with a backslash (/).

Delete Directories with rmdir (Terminal)

This is the well-known command-line utility to remove a directory. If you want o delete any empty directories in your current working directory then you can simply use this command to delete them.

To delete any directory first you have to navigate to its containing directory and write this command according to its name. For example, if you want to delete the test1 directory inside the Download directory at first you have to navigate to the Download directory and write a command like this:

rmdir test1

If the directory contains any files, your terminal will print the following error:

rmdir: failed to remove 'test1': No such file or directory
  

In this case, you have to delete the folder contents and run this command again. Or, you can use the rm command with the recursive flag. Because the rmdir dose does not allow to deletion of a directory with contents.

Delete Directories Using rm (Terminal)

This is another Utility to delete a directory in your Linux  System. If you want to delete any directories that contained any files or other contents, it can be the best option for you.

By default, when you use this utility without any option, it does not remove any directories. If you want a directory you have to add the -d flag with the rm command. Or, if you want to delete a folder with contents, you need to use the -r recursive flag instead of -d flag.

For example, If you want to delete a directory named test2 along with all of its contents you would type:

rm -r test2

This is a small utility but powerful yet you should be careful when using it. Because there is a chance that your deleted directory contains some necessary files or information.

Delete Directories with find (Terminal)

This command-line utility allows you to search for files and directories based on a given expression and perform an action on each matched file or directory.

The common use of find the utility is to search directories and deletes directories on several conditions. For example, if you want to delete all of the directories that end with _suffix in the current working directory then you have to run a command like this:

find . -type d -name '*_suffix' -exec rm -r {} +

Let’s breakdown the command above:

  • find a Linux command line utility to conditionally traverse file hierarchy.
  • . indicate that this command will run inside the current and absolute directory.
  • -type d – a flag that indicates restricts the search to directories.
  • -name '*_suffix' – Search only directories that end with _cache
  • -exec – executes another command at once with the condition, in this case, that is rm -r.
  • {} + – appends the matched directories to the end of the rm command.

Delete Directories in Linux using Desktop File Manager (GUI)

If you are not comfortable with the command-line interface you can try this one. This is the simplest way to a directory in Linux and you do not need any type of extra Linux Knowledge.

  1. To delete a directory, open the File Manager and navigate according to the directory that you want to delete.
  2. Now Right-click on the select and select “Move to Trash“.
  3. You can select multiple directories and files by pressing the “Ctrl” key at once.
  4. Now you can press the delete button from your keyboard to delete selected directories.

If you select the Delete option, it will warn you that you are going to delete the directory permanently. If you do not want the file anymore, then you can click ok/delete. this is the irreversible method like a command-line utility.

Conclusion

This is the complete guide on how How to delete a directory in Linux. If you follow this article you will be able to delete any directory in your Linux system. However, you are using the terminal or any graphical file manager.

In this article, I tried to explain  rm and find command-line utility to delete a directory based on a different state. However, deleting directories in Linux is a simple and easy process, but you must be cautious not to delete important data.