How to Remove Directory and its Contents in Linux?

In certain situations, a Linux user may require to delete some useless directories and their content. Usually, this practice is followed to free up some space on the system by removing old/useless directories and their content. Considering its importance, this post will address the possible method to remove a directory and its content in Linux.

How to Remove Directory and its Contents in Linux Using Terminal?

In Ubuntu, the terminal provides an interface to execute the commands. To remove directories, there are two commands, i.e., “rm” and “rmdir”. The “rmdir” command only refers to removing the empty directories. Whereas the “rm” command can be used in this case. The syntax of the “rm” command is provided below:


$ rm -r [directory]

The “rm” command is utilized to remove single or multiple directories specifying their names. Different functionalities of the “rm” command are explained below:

Commands     Description
rm -r dir_nameIt represents deleting the directory and its content.
rm -r dir_name dir_name1It identifies removing multiple directories with their content.
rm -d dir_nameIt is utilized to remove an empty directory.

Let’s see the practical implementation of the above commands:

Example 1: Remove an Empty Directory

An example is carried out to remove an empty directory through the terminal. After that, The command to delete the directory is given below:

$ rm -d Dir2

The output verifies through the “ls” command that the particular directory “Dir2” has been deleted successfully.

Example 2: Remove Directory and its Content

An example is considered to remove the specific directory. To do so, the “-r” utility is used to delete all content present in the directory:

Display Directories

To display all directories which are located in the Home directory is possible through the “ls” command:

$ ls

Remove Directory and its Contents

To remove the specific directory, users must specify the name of the directory with the “rm” command. In our case, specify the directory name “Dir1” as seen below:

$ rm -r Dir1

The output verifies that the “Dir1” directory has been successfully deleted.

Example 3: Remove Directory Content Using Path

Another example is adapted to remove the specific content stored in the directory via the “rm” command. To do so, the script is given below:

$ rm Dir1/itslinuxfoss.txt

Now, you can verify through the “ls” command that the particular file is not present in the “Dir1” directory.

$ cd Dir1
$ ls

Let’s take another example of removing multiple directories.

Example 4: Remove Multiple Directories and Their Content

This example assists in removing multiple directories quickly through the terminal.In our system, we have multiple directories that can be seen in the terminal using the “ls” command:

$ ls

To remove the multiple directories, the “rm” command is utilized with “-r” that forcefully removes the directory with their content:

$ rm -r 'Directory 1' 'Directory 2'

Using the “ls” command, you can verify that multiple directories have been successfully deleted from the Linux as seen below:

$ ls

Example 5: Remove Sub-Directory and its Content

To remove the subdirectory and its content, you can visualize the content in the “Dir1” directory on which the subdirectory “Dir2” is found:

$ cd Dir1
$ ls

We target the “Dir2” sub-directory present in the “Dir1” directory. For this, execute the below script from the Home Directory:

$ rm -r  Dir1/Dir2

You can verify through the “ls” command in the “Dir1” directory that the targeted subdirectory “Dir2” has been deleted:

$ ls

That is all from this Linux post.


In Linux, the “rm -r dir_name” command removes a directory and all its content from the operating system. Additionally, users can remove single or multiple directories by specifying their names. This guide has covered all the essential steps to remove a directory and its content in Linux.