How to Delete a Directory in Linux? (All Commands)


Reading Time: 3 minutes

Linux has lots of commands to learn. This how-to guide will explain to you how to Delete a Directory in Linux.

Do you need to delete a directory in Linux? what is a directory? how you can delete it? you know, a directory is a group of files. Linux command lines let you delete directories with different conditions. For example, you can delete empty directories or search and delete them by name, or select a directory and delete it with all its content.

Linux VPS Hosting

Get yourself an economy or premium Linux VPS for hosting your website or remote desktop, at the cheapest price out there. VPS Running on Linux KVM for increased efficiency and working on powerful hardware with NVMe SSD storage for increased speed.

Check Out Linux VPS Plans
Linux VPS Hosting

there are two commands to delete a file in Linux including the rmdir command and rm command. Here we explain the most popular possibilities for these commands:

How to Delete an Empty Directory in Linux?

If you have an empty directory in Linux that you want to delete, use the rmdircommand. This command is useful because it only removes the empty directory. Run the command as:

rmdir dir-name

In which dir-name is the name of your directory.

If the directory is not empty, you will receive the message below:

rmdir: dir-name: Directory not empty

How to Delete the Directory, File, and Subdirectory in Linux?

If you want to delete the directory, regardless of its content, you need to use the rm command. Keep in mind that the removal will be permanent.

$ rm -r dir-name

Linux tries to confirm the deletion of the write-protected files. To skip the confirmation, prompt the command below:

$ rm -rf dir-name

If you want to receive one confirmation, use the command below:

$ rm -rI dir-name

To remove multiple directories and their content, use the command below. This command removes directories dir1, dir2, and dir3.

$ rm -r dir1 dir2 dir3

You can also name-match and delete directories based on that. In the example below, you delete every directory starting with dir.

rm –r dir*

Remember only owners can delete their directories. If you are the sysadmin, prompt the command bellow:

sudo rmdir /path/dir/

sudo rm -rf dir-name

How Do I Delete All Files and Subdirectories in a Directory in Linux?

Do you want to keep the directory but remove all its content?

To delete all files within the directory, run:

rm /path/to/directory/*

To delete files, subdirectories, and hidden files and directories, run:

rm -r /path/to/directory/*

Delete a Directory in Linux by Using the Find Command

You can prompt the system to search for certain directories and delete them. For example, the command below finds all directories named dir and deletes them:

find . -type d -iname ‘dir’ –delete
  • -type d : Only search within directories.
  • -iname ‘dir’ : Search for directories named ‘dir’.
  • -delete : Delete all found directories.

Find and Remove All Empty Directories in Linux

You can use the find command to delete all empty categories at once. For example, the line below searches a directory name dir and deletes all empty folders:

find . -type d -iname ‘dir’ -empty –delete
  • -type d : Only search within directories.
  • -iname ‘dir’ : Only search within the directory named ‘dir’.
  • -empty : Search for empty directories.
  • -delete : Delete all found empty directories.

What to Do If Getting Permission Denied Error Message While Removing a Directory?

Owners and sub-admins can delete a directory in Linux. If you delete a directory and receive a permission denied error, run the following syntax:

sudo rmdir /path/to/dir/ sudo rm -rf dir2

When prompted, you need to provide a root user or sudo user password.


Linux allows you to not only delete a directory, but also search for the empty directory with a specific name, and delete them. You can also keep the directory, but delete all its content. All this is possible using the commands above. In this article, we explained how to delete a directory in Linux. Further, we showed you how to delete the file, and subdirectory in Linux.

Master student in computer software engineering Intrested in programming, web developing, and cloud computing


Leave A Comment

Leave a Reply

Your email address will not be published. Required fields are marked *