A Complete Guide to Delete a Directory in Linux [rm & rmdir]

Removing a directory in Linux is sometimes unavoidable. This article explains the command to delete a directory in Linux.

how to remove directory in linux

Do you have unwanted directories cluttering up your Linux server? Are you looking for an easy and efficient way to delete them? This article will walk you through all the commands for removing a directory in Linux. By the end, you’ll know how to delete directories using the Linux command line; let’s take a look.


  • A system that runs the Linux distribution
  • Access to the command line and terminal window
  • An account with sudo privileges

Part 1. Commands to Delete Directory in Linux – rm and rmdir

If you want to remove a directory in Linux, you need to learn the two Linux commands you can use in the terminal window or command line. These are the ‘rm’ command and the ‘rmdir’ command.

1. rmdir – Remove Empty Directory in Linux

The rmdir command is used in the terminal window or command line to remove only empty directories. This means that if the directory contains files or subdirectories, this command will not work. If you want to delete a directory containing subdirectories, folders, and files, then the rm command is what you’re looking for.

The command also includes the following options:

--ignore-fail-on-non-empty This means it won’t show an error message when removing a non-empty directory.
-p This option removes the directory along with its parent in the hierarchy
-v It provides a verbose output by listing the removed directories
--help Display help text
--version It displays the command version

2. rm – Remove Directory With Contents in Linux

On the other hand, the rm command is a Unix command that removes files and directories. It stands for ‘remove,’ and you can use it to delete one or more files, folders, and directories.

This command line operates on some given set of files or directory names to remove them from the file system. It is a non-interactive command, so it will remove these directories without asking for confirmation. There are different options available with the rm option that you can use to avoid accidental deletion, and they include the following:

Options and Description

-i It opens the interactive mode and prompts for confirmation before you delete each file
-f This option forces the removal of directories and files without requesting confirmation
-r This opens the recursive mode and removes the specified directory, including its content
-v This is the verbose name and usually displays the name of directories being removed
-d This command removes empty directories
-I It asks for confirmation before removing more than three files
--help This option displays the help text
--version This command displays the command version

Note: The rm and rmdir command permanently remove directories without first transferring them to the trash directory. Therefore, you cannot restore the directory removed with this command.

Part 2. How To Remove an Empty Directory Using the rmdir Command


rmdir [options] [directory_name]

You should replace directory_name with the name of the directory you want to remove. 


When you use rmdir command to remove a non-empty directory, you will receive the error message “rmdir: failed to remove ‘Example’: Directory not empty.

delete directory on linux rmdir error

In this case, the Test subdirectory is found inside the Example directory.

reasons for rmdir error

If you want to remove such directories using the rmdir command, you can add them in reverse other of the hierarchy and ensure it is listed using the -v option. An example is: 

rmdir -v Example/Test Example

rmdir v to remove directory and contents in linux

Alternatively, you can use the –p option. It removes the subdirectory and its hierarchical parent. An example is:

rmdir -p -v Example/Test

rmdir p to remove directory and subdirectories in linux

You can also choose to remove multiple directories with the same name. However, you have to use wildcards. An example is if you want to remove directories names Example1, Example2, and Example3. You would enter the command:

rmdir -v Example*

This command would list all the directories with the same name to be deleted.

rmdir list multiple directories to delete

Part 3. How To Remove a Directory With Contents and Subdirectories Using rm Command

We’ve already established that the rm command removes directories with contents and subdirectories.


rm[options] [file or directory name]

If you need to delete a directory whose name starts with a hyphen (-), the syntax should be:

rm -- [directory name]


rm ./[directory name]

If you want to remove multiple content and subdirectories using the rm command, you should add multiple directory names or files but separate them by blank spaces.

Note: When you use the rm command without any options to remove a directory, you will receive an error message shown below. This means, you must add an option to remove a directory on Linux using rm.

remove directory in linux rm error


  1. If you want to delete an empty directory from your Linux computer, add the -d option to the command. An example is:

rm -d Example

remove directory using rm command
  1. If you want to eliminate an entire directory, including its folders and subdirectories, while listing each step of the process, use the Linux rm directory deletion command:

rm -r -v Example

recursive command to delete linux directory
  1. Adding the -i option results in a prompt asking for confirmation before removing the directory. An example is:

rm -d -i Example

To proceed, type and then press ‘Enter’ to confirm.

confirmation to remove directory in linux
  1. To remove write-protected directories, you need to provide user input. Create such directories using the command: 

sudo mkdir TestDirectory

Now, to remove this directory, use this command in Linux to delete directory and content: 

rm -d Example

command for removing protected directories

Afterward, type and press Enter to confirm the decision to delete. The -f flag removes the need for confirmation and is used like this: 

rm -d -f TestDirectory

  1. If the write-protected directory contains more content and subdirectories, you can use this command in Linux to remove directory and contents: 

rm -rf <directory name>

Note: The command rm -rf to remove directory in Linux is quite dangerous because it forces a recursive deletion on the root directory. This process will render your system unusable. Additionally, don’t use the -f and sudo when removing directories using the rm command unless you’re very sure of your actions.

Part 4. How To Find and Delete Unwanted Directories in Linux

If you can’t find the directory you would like to delete, you can use the find command in conjunction with the rm command to delete the directories you don’t want. This command searches for directories and files in specific locations, and then the rm command works to remove the unwanted directories. An example of using the find command to remove an unwanted directory in Linux is: 

find . -type d -name “name” -exec rm -r {} +


  • ‘.’ Specifies the current directory as the beginning location of the search.
  • ‘-type d’ restricts your search to directories alone.
  • ‘-name “temp”’ specifies the name pattern the search results should match.
  • ‘exec rm -r {} +’ executed the rm command on all directories found using the find command.

Alternatively, you can run the command below to find and delete all directories named temp and delete them in the present directory.

$ find . -type d -iname ‘temp’ -delete 

If your focus is to find and delete unwanted empty directories, then run the command: 

$ find . -type d -name ‘temp -empty -delete


  • ‘type d’ focuses the search on directories and ignores other files.
  • ‘-iname ‘temp’ focuses the search on directories names temp. You can also use wildcards in this search.
  • ‘-empty’ means you’re only searching for empty directories.
  • ‘-delete’ deletes all the empty directories matching the search name found. 

It’s essential to express caution when using the find command in conjunction with the rm command because deleted files are lost entirely. 

Part 5. What To Do If You Receive the Permission Denied Error Message While Removing Directories?

Sometimes, the directory you want to delete requires write permission before you can remove them. This could be because you don’t have root privileges or because you don’t own the directory. Luckily for you, several fixes exist, and the solutions are outlined below.

Step 1: First, check to confirm you can delete the directory using the ‘Is -I’  You’ll need to write permission for the directory. If you don’t, you must change the permission before you can proceed to deletion.

Step 2: Next, you can use sudo to gainroot privileges. Use ‘sudo’ to run the command as the root user. An example of the command to use is ‘sudo rm -rf directory.’ Using the -rf option forces the deletion of the directory and all its content.

Step 3: If the directory isn’t yours, you have to change ownership before deletion using the command line ‘sudo chown username:groupname directory.’ This should change the ownership and allow you to delete the directory.

Step 4: You can also boot your PC in single-user mode; this allows only one user to log in and have root privilege so you can easily delete the directory.

Step 5: Finally, when all else fails, use a live CD or bootable USB to boot in a different operating system and remove the directory. 

Part 6. [Optional] Delete a Directory in Linux Using File Manager (GUI)

When you want to delete files in a folder, then deleting the entire directory saves you the time to delete them one at a time. This also provides you with the option of removing the subdirectories as well. An option you can use instead of the bash remove directory commands is the file manager window in Linux. Below are the steps to delete a directory in Linux using the File Manager.

Step 1: Launch File Manager on your Linux PC to begin the process.

Step 2: Next, locate the directory you would like to delete. You can use the Search feature on file manager to find the files or simply navigate to the location of the directory.

search file manager

Step 3: Once you locate the directory you want to use, right-click the directory and hit the Move to Trash option. Automatically, the directory will be moved to the trash folder, where it will temporarily store it.

move directory to linux trash

Step 4: You might have to confirm your decision to delete the directory and click the OK or Delete option to remove the directory.

Step 5: To permanently delete the directory from your Linux device, go to the Trash location and click the Empty Trash option at the top of the window. If you only want to remove the directory, right-click on the directory in the trash folder and click Delete from Trash.

remove directory from trash

Step 6: You might have to confirm your option to permanently delete the directory, click OK or Delete when the dialog box appears.

This will permanently delete the directory from your Linux PC. Once you empty the directory from the trash folder, you can’t recover it again, so be careful when deleting directories. 

Part 7. How To Undelete Accidentally Removed Directory in Linux

Have you mistakenly deleted a directory in Linux using the rm or rmdir command? Don’t worry; There are multiple Linux data recovery tools available for you and Recoverit Linux Recovery is one of the most recommended among them. It safely scans your Linux PC through a remote connection and recovers the directories you accidentally deleted in a few simple clicks.

Wondershare Recoverit - Your Safe and Reliable Linux Recovery Software

5,481,435 people have downloaded it.

Recovers lost or deleted documents, photos, videos, music, emails, and other 1000+ file types from Linux effectively and completely.

Compatible with all popular Linux distros, including Ubuntu, Linux Mint, Debian, Fedora, Solus, Opensuse, Manjaro, etc.

Assists in 500+ data loss scenarios, such as deletion, disk formatting, OS crash, power outage, virus attack, lost partition, and many more.

Offers multiple filtering options to narrow your search and find the directory or file you’re looking for faster.

Additionally, recovering your deleted directory is relatively easy; the steps are outlined below:

Free Download

For Windows Vista/7/8/10/11

Free Download

For macOS X 10.10 or later

Step 1: Start Linux Recovery

Launch Recoverit after installing it on your PC. On the home page, choose NAS and Linux > Linux Recovery from the menu.

choose linux recovery

Step 2: Connect to Linux Device

A new Window will appear asking you to provide your Linux details to create a remote connection. Once you provide the information needed, click the Connect button.

create a remote connection

Step 3. Scan for Deleted Directory

Once the connection is established, Recoverit will begin an automatic scan to search for the lost directory and files in your Linux PC. You can narrow the search using various filter options like date modification, file format, etc.

scan for deleted directory in linux

Step 4. Preview and Recover

Once you find the directory you need, you can preview some files in the directory to confirm they’re what you’re looking for. Finally, click the Recover button to restore the files or directory to a safe location.

preview and recover deleted files and directory
Free Download

For Windows Vista/7/8/10/11

Free Download

For macOS X 10.10 or later

Removing Directory and Contents in Linux FAQs

The rm and rmdir commands can remove a directory in Linux. However, the rmdir only removes empty directories, while the rm command removes directories with files and subdirectories.

Yes, you can. You can do this using the rm remove directory Linux command. When used with the -r command, it removes the directory and all its contents, including the subdirectories and files.

Deleting all the files and folders in a particular directory in Linux involves using the rm command with the -r option.


Without the right guide, removing directories in Linux would be difficult. Luckily, this guide breaks down all you need to remove on removing directories in Linux. We’ve provided all the commands you need and how to remove directories in File Manager. By this point, you should understand how to use the rm and rmdir commands to remove files and directories from your Linux PC safely.

You May Also Like

Best Alternatives to Recuva for Linux

This article introduces Recuva and its limitations while offering its best alternatives.

How To Undelete Files Using debugfs in Linux

A guide to recover deleted files using debugfs - a Linux interactive file system debugger.

How To Recover Data From LVM Partitions on Linux

Learn step-by-step procedures to restore data from LVM partitions on Linux systems.

How To Undo rm in Linux

What can you do to protect Linux data from the accidental running of the RM command?