How to Delete a Directory in Linux

Channel: Linux
Abstract: pass the -d option as shown below. $ sudo rm -d mydirectory Using find command Find command is a command-line tool that helps users search for files

Earlier in one of the tutorials, we have explained how to create directory in Linux. Now let's check how to delete a directory in Linux which is either empty or having subdirectories with files. This is especially when you need to free up some space on your system in order to save more files or install additional packages.

There are many ways in which you can remove a directory in Linux. You can make use of the file manager if you are using a GUI system such as GNOME, MATE or KDE Plasma, or you can do it over the terminal.

When working with a GUI system deleting a directory takes it to the crash can, the equivalent of recycle bin in Windows from where it can be restored. However, the scenario is different when working on a command line on a minimal system because once a directory is deleted, it is permanently removed and cannot be recovered.

How to Delete Data From MySQL Using...

To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video

How to Delete Data From MySQL Using PHP

This tutorial will take you through various ways in which you can delete a directory in Linux.

Delete a directory using rmdir command

The rmdir command, short for'remove directory', is a command-line tool that is used to delete empty directories. The operation will be successful if and only if the directory is empty. The syntax for deleting a directory is as follows:

$ sudo rmdir directory_name

For instance, to remove an empty directory called 'mydirectory', run the command:

$ sudo rmdir mydirectory

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

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

The error is a clear indication that the directory contains either files or folders or both.

Remove a directory using rm command

Short for remove, the rm command is used for deleting both empty and non-empty directories.

The rm command is usually used for removing files in Linux. However, you can pass some arguments that can help you delete directories. For example, to remove a directory recursively ( remove the directory alongside its contents), use the recursive option  -r (-R or --recursive) as shown below.

$ sudo rm -r mydirectory

If a directory is write-protected, you will be prompted whether to continue deleting the files inside the directory and the directory as a whole. To save you the annoyance and inconvenience of constantly bumping into such prompts, add the  -f option to force the deletion without being prompted.

$ sudo rm -rf mydirectory

Additionally, you can delete multiple directories at a go in a single command as shown in the command below. The command deletes all the directories and their subdirectories without prompting for deletion.

$ sudo rm -rf mydirectory1 mydirectory2 mydirectory3

To exercise more caution, you can use the -i option which prompts for the deletion of the directories and subdirectories. However, as we saw earlier, this can be quite annoying especially if you have several subfolders and files. To address this inconvenience, use the -I flag to prompt you only once.

$ sudo rm -rI mydirectory/

When you hit y for 'Yes', the command will remove all the subfolders and files in the directory without prompting any further.

To remove an empty directory, pass the -d option as shown below.

$ sudo  rm -d mydirectory
Using find command

Find command is a command-line tool that helps users search for files as well as directories based on specific search criteria/pattern or expression. Additionally, the command can be used to search for directories and delete them based on the specified search criteria.

For example, to delete a directory called 'mydirectory' in the current directory, run the command below.

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

Let’s break down the parameters in the command

( . ) - This denotes the directory in which the search operation is being carried out. If you want to carry out the search in your current directory use the period sign (.)

-type d  - This sets the search operation to search for directories only.

-name  - This specifies the name of the directory.

-exec rm -rf -  This deletes all directories and their contents.

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

Let’s take another example:

Remove an empty directory

If you wish to remove all empty directories use the following command:

$ sudo find. -type d -empty -delete

Again, let’s break this down

.  - This recursively searches in the current working directory

-type d - This keeps the search to directories only

-empty - This restricts the search pattern to empty directories only

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

If you have lots of empty directories then use a shell script to delete empty directory.

Conclusion

In this tutorial, we looked at how to delete a directory in Linux using the rm, rmdir and find commands. We hope you can comfortably delete a directory in Linux whether it contains files and other subdirectories, or simply if it is empty. Give it a try and get back to us with your feedback.

Ref From: linoxide
Channels:

Related articles