How to delete a directory in Linux

Earlier in this tutorial, we have explained how to create directories in Linux. Now, let’s check how to delete a directory in Linux that is empty or has a file subdirectory. This is especially true when you need to free up some space on your system to hold more files or install other software packages.

In Linux, there are several ways to delete directories. If you are using a GUI system (such as GNOME, MATE, or KDE Plasma), you can use the file manager or operate on the terminal.

In the case of a GUI system, deleting the directory will crash the directory, which is equivalent to the recycle bin in Windows, and you can restore the directory from it. However, the situation is different when working on the command line on a minimal system, because once a directory is deleted, the directory is permanently deleted and cannot be recovered.

This tutorial will take you through various ways to delete directories in Linux.

Delete directory with rmdir command

The rmdir command is an abbreviation for “Delete Directory” and is a command line tool for deleting empty directories. The operation succeeds if and only if the directory is empty. The syntax for deleting a directory is as follows:

$ sudo rmdir directory_name

For example, to delete an empty directory named “mydirectory”, run the following command:

$ sudo rmdir mydirectory

If the directory is not empty, an error is displayed on the screen as shown below:

'rmdir: failed to remove ‘mydirectory’: Directory not empty'

Delete directory in Linux

The error clearly indicates that the directory contains files or folders, or both.

Delete directory with rm command

Abbreviation for the rm command, used to delete empty directories and non-empty directories.

The rm command is usually used to delete files in Linux. However, you can pass parameters that help you delete the directory. For example, to delete directories recursively (deleting directories next to the directory and its contents), use the recursive option -r (-R or –recursive) as shown below.

$ sudo rm -r mydirectory

Delete directory

If the directory is write-protected, you will be prompted whether to continue deleting files in the directory and the entire directory. To save you the constant annoyance and inconvenience of such tips, please add -f Options are forcibly deleted without prompting.

$ sudo rm -rf mydirectory

Force directory deletion in Linux

In addition, you can delete multiple directories at once in one command, as shown in the following command. This command deletes all directories and their subdirectories without prompting you to delete.

$ sudo rm -rf mydirectory1 mydirectory2 mydirectory3

Delete multiple directories

To be more cautious, you can use -i This option prompts to delete directories and subdirectories. However, as we saw earlier, this can be very annoying, especially if you have multiple subfolders and files. To resolve this inconvenience, use -I The flag reminds you only once.

$ sudo rm -rI mydirectory/

Delete directory without prompting multiple times

When you hit y If set to Yes, this command deletes all subfolders and files in the directory without further prompting.

To delete an empty directory, pass -d The options are shown below.

$ sudo  rm -d mydirectory

Delete empty directory

Use the find command

The find command is a command line tool that helps users search for files and directories based on specific search conditions / patterns or expressions. In addition, this command can be used to search the directory and delete it based on specified search criteria.

For example, to delete a directory named “mydirectory” from the current directory, run the following command.

$ sudo find . -type d -name "mydirectory" -exec rm -rf {} +

Delete directory using find command

Let’s break down the parameters in the command

(.)-This indicates the directory in which the search operation is performed. If you want to search in the current directory, use a period (.).

-type d- Sets the search operation to search directories only.

-name-This specifies the name of the directory.

-exec rm -rf- This will delete all directories and their contents.

{} +-This will append all files found at the end of the rm command.

Another example:

Delete an empty directory

If you want to delete all empty directories, use the following command:

$ sudo find. -type d -empty -delete

Delete directory in Linux

Again, let’s break it down

. -This will search recursively in the current working directory

-d- Keep search only to directories

-empty- Limit search mode to only empty directories

-delete- This will delete all empty directories found, including subdirectories.

If you have a lot of empty directories, use a shell script to delete the empty directories.

in conclusion

In this tutorial, we have studied how to delete directories in Linux using the rm, rmdir and find commands. We hope you can easily delete a directory in Linux, whether that directory contains files and other subdirectories, or only empty directories. Please try it out and contact us with your feedback.

Sidebar