How ToLinux

How to Remove/Delete Directory in Linux

If you’re a new user to Linux, you should know the ways to delete or remove the unwanted directories or folders. Unlike Windows and macOS, it is not easy to delete a directory in your Linux. If you’re using a GUI system, you can make use of the File Manager to delete or remove the directory. On the other hand, if you want to remove the multiple directories from your Linux, you can go for the command line method.

Removing the unwanted directories will free up some space on your system. Hence, it is necessary to know those commands to delete or remove the directories.

NOTE: While removing the directory with the help of desktop file manager, the directory will be moved to the trash folder and you can restore it whenever you want. However, the directory will be deleted permanently while using the command line.

How to Delete or Remove an Empty Directory in Linux?

To delete an empty directory on your Linux, you can use the rmdir command. Here is the syntax to delete an empty directory

rmdir dirname

For example, if your directory is named as techowns, type the type following command on your terminal to delete or remove that directory.

rmdir techowns

How to Remove/Delete Directory in Linux?
Image Source: Techieshelp

If your directory is not empty, you will receive the following error message

rmdir: failed to remove ‘techowns’: No such file or directory

In that case, use the method that we mentioned below.

Techowns Tip: How to rename a file on Linux?

How to Remove or Delete a non-Empty Directory in Linux?

To delete a non-empty directory, you have to use the rm command. Here is the syntax

rm -r dirname

To delete a directory named techowns, type

rm -r techowns

You can also use the rm command to delete the empty directory and single files.

To remove a single file, you have to use rm without any additional commands. Let assume a file named techowns and to delete that, type the following command in the terminal

rm techowns

How to Remove or delete multiple directories in Linux?

In case, if you want to delete more than one directory simultaneously, you can use the same rm command to delete those directories.

Here is the syntax to delete three directories named techowns1, techowns2 and techowns3

rm -r techowns1 techowns2 techowns3

Remove Directory using Find Command

Linux users can also utilize find command to find and delete the directories based on search criteria.

Here is the syntax to delete all the directories which end with techowns in a current directory

find . -type d -name ‘*techowns’ -exec rm -r {} +

(.)Represents the directory in which the search option is carried out
-type drestricts the operation to search for the directory only
-namerepresents the name
-exec rm -rf to delete the directories and its files
{} +adds directories to the rm command

Deleting directories in Linux doesn’t require rocket science. You just need to know the commands and their respective actions. As we said earlier, you can’t restore the files while using the command line. So, make sure that you aren’t deleting any personal or important data from your system.

Want to share anything about the article? Use the comments section provided below. Also, follow our Facebook and Twitter profiles to get the latest updates instantly.

Was this article helpful?
YesNo

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button