how to delete a directory in linux

The directory is 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 terminal and Graphical User Interface.

If you are using a desktop file manager like Gnom’s Files or KDE Dolphin, you will 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 at first you have to navigate on 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  on Download directory and write a command like this:

rmdir test1

If the directory is not empty, you will get the following error:

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

In this case, you will need to use the rm command or manually remove the directory contents before you can delete it.

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. To delete an empty directory, use the -d (--dir) option and to delete a non-empty directory, and all of its contents use the -r (--recursive or -R) option.

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 search directories and deletes directories on several conditions. For example, if you want to delete all of the directories that end with _tmp in the current working directory then you have to run a command like this:

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

Let’s breakdown the command above:

  • /dir – recursively search in the current working directory (.).
  • -type d – restricts the search to directories.
  • -name '*_cache' – search only directories that end with _cache
  • -exec – executes an external command with optional arguments, in this case, that is rm -r.
  • {} + – appends the found files 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 to delete it will ask you to Delete permanently? If you doot the file anymore then you can click ok. 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 able to delete any directory in your Linux system. However, you are using the terminal or any graphical file manager.

In this article, I had tried to explain  rm and find command-line utility to delete 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.

If you like this article, share it with your friends…

Speak your mind!