Phone :  +370 (5) 204-1903
Email :
  1. Dedicated server
  2. Blog
  3. How to delete directory in Linux?

How to delete directory in Linux?

In Linux, how do I remove a directory? This is a typical query from Linux novices. It's certain that as you get used to using the Linux operating system, you'll wish to remove a directory.

Linux Tutorial May 05, 23 by Nisal N 5 min Read
How to delete directory in Linux?

List of content you will read in this article:

If you use the GUI, removing a directory in Linux is a rather straightforward process. However, you can also delete directories via terminal commands if you don't have access to the GUI.

In this article, we'll demonstrate how to use the command line or terminal window to remove a directory in Linux.

Although deleting directories is straightforward, you must not destroy any crucial data.

Before you start, you need a machine that runs a Linux distribution, a user account with sudo rights, and access to the command line or terminal window.

How to use the rm command to delete a directory in Linux?

In Linux, the rm command is used to remove files and directories. This command can be used to completely delete a directory, which deletes the directory together with all of its files and subdirectories.

The syntax for this command is as follows:

rm [options] [files and/or directories]

Add several file or directory names, separated by blank spaces, to the rm command to remove numerous files or directories.

Don’t forget that an error message appears when attempting to remove a directory using the rm command without any parameters.

The rm command can be modified by adding the -d parameter to remove an empty directory:

rm -d Example

To remove a directory with files and subdirectories, use the -r flag.

When the -v flag is used, the output describes each stage of the operation, whereas the -r flag deletes the entire directory, including subdirectories and files:

rm -r -v Example

A prompt requesting you to confirm directory removal is displayed when you use the -i option. To confirm, enter Y and then hit Enter.

rm -d -i Example

When removed, write-protected directories demand user input. Make such a directory by using:

sudo mkdir Example

To delete a directory in Linux, use the following:

 rm -d Example

To confirm the deletion, enter Y and then hit Enter. Use the -f switch or increase the command privileges to sudo to avoid the confirmation:

rm -d -f Example

sudo rm -d Example

Use the following command if there are other files and directories in the write-protected directory:

rm -rf <directory name>

rm -rf / makes your system unusable by forcing a recursive deletion on the root directory.

When removing folders, avoid using sudo and -f unless you are sure what you are doing.

Removing a directory in Linux using rmdir command

Empty directories can be removed with the rmdir command:

rmdir [options] [directory name]

The command equals rm with the -d flag: rm -d.

You receive the following error when using rmdir on a non-empty directory: 

RMDIR: [folder]: Not an empty directory.

Use this command without any parameters to remove an empty directory:

rmdir test

The -p flag of the rmdir command enables you to remove a directory with its parent in the tree. For instance, imagine your file system looks like this:

> Test

---> Test22

The Test2 subfolder is located in the directory Test. The test becomes an empty directory if the Test2 directory is removed. Thus as opposed to:

rmdir Test/Test2 Test

# deleting Test2 and then Test

Use the -p flag as follows:

rmdir -p Test/Test2

With this command, Test2 and its parent, Test, will be deleted from the tree. But if either directory is not empty, this command will fail.

Removing a directory in Linux with find

With the help of the command-line tool named find, you may look for directories based on an expression and take action on each matching directory.

The most typical scenario is to delete directories based on a pattern using the find command. 

Run the following command, for instance, to remove all folders in the current working directory that finish in the character _cache:

find . -type d -name '*_cache' -exec rm -r {} +

You can run the following to clear every empty directory in a directory tree:

find /dir -type d -empty –delete

Be careful when you decide to use the -delete option. If the -delete option is added first, the search command will erase everything below the beginning points you specified. The find command line is processed as an expression.

Use -delete as the last option and always test the command without it.


After reading this guide, you should be able to use the terminal window or command line to delete a directory in Linux.

Linux allows you to remove directories using the commands rm and rmdir. Non-empty folders are deleted by rm. Empty directories are removed by the rmdir command. However, this command cannot delete a directory with files inside of it.

When you want to delete a folder, you might be used to dragging it to the trash on your computer. You can store the documents you're about to destroy in the trash. You have to empty your trash can before you remove the file.

However, you should be aware that there is no trash can on the Linux command line. After removing a directory in Linux, it’s permanently erased.

People also read:

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment