How to Delete a Directory and folder in Linux?

Having troubles removing a folder or directory in Ubuntu?

This guide is for you.

Deleting a directory or folder in Linux which either have an empty or with subdirectories containing files is important to free up some space on your system in order to save more files or install additional packages.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Ubuntu related tasks.

In this context, we shall look into different ways in which you can delete a directory in Linux.

How to delete a directory or folder using rmdir command ?

The rmdir command, acronyms 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 name_of_directory

For instance, to remove an empty directory called 'home_directory', execute the command:

$ sudo rmdir home_directory

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

rmdir: failed to remove 'home_directory': Directory not empty

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

How to remove a directory using rm command ?

The rm command is an abbreviation for 'remove'. It 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 home_directory

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 home_directory

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 home_directory1 home_directory2 home_directory3

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 home_directory/

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 home_directory

How to use 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 'home_directory' in the current directory, run the command below.

$ sudo find . -type d -name "home_directory" -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:

How to remove an empty directory or folder ?

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

$ sudo find. -type d -empty -delete

Also, let us 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.

[Need to harden your Linux Server to improve Security? We are available to help you today. ]


This article will guide you on how to delete a #directory in #Linux using the #rm, rmdir and find commands. It is quite easy to delete a directory in Linux whether it contains files and other #subdirectories, or simply if it is empty. To delete (i.e. remove) a directory and all the sub-directories and files that it contains, navigate to its parent directory, and then use the command rm -r followed by the name of the directory you want to delete (e.g. rm -r directory-name ). The “-rf” flag, along with the “rm” command, removes a directory recursively without prompting the user for confirmation.