delete a directory in Linux

How to delete a directory in Linux?

Today’s topic is how to delete a directory in Linux. There are many commands to learn in Linux. This guide will explain to you how to delete a directory in Linux. Should you delete a directory in Linux? What is a directory? How can you remove it? As you know, a directory is a group of files.

The Linux command line allows you to delete directories with various conditions. For example, you can delete empty directories or search and delete by name, or select a directory and delete all its contents.

There are two commands to delete a file in Linux which are “rmdir” command and “rm” command. Here we explain the most popular options for these commands.

How to delete an empty directory in Linux?

How to delete an empty directory in Linux?

If you have an empty directory in Linux you want to delete, use this command:

rmdir

This command is useful because it only deletes empty directories. Run the command like this:

rmdir dir-name

where “dir-name” is the name of your directory. If the directory is not empty, you will get the following message:

rmdir: dir-name: Directory not empty

How to delete directories, files, and subdirectories in Linux?

If you want to delete the directory, regardless of its contents, you need to use the “rm” command. Note that the removal will be permanent.

rm -r dir-name

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

rm -rf dir-name

If you want to get a confirmation, use the command below:

rm -rI dir-name

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

rm -r dir1 dir2 dir3

You can match and delete directory names based on this. In the example below, you would delete each directory starting with dir.

rm –r dir*

Note that only owners can delete their directory. If you are a sysadmin, issue the command below:

sudo rmdir /path/dir/
sudo rm -rf dir-name

How can I delete all files and subdirectories in a directory in Linux?

Do you want to keep the directory but delete all its contents? To remove all the files in a directory, run:

rm /path/to/directory/*

To remove files, subdirectories, and hidden files and directories, execute:

rm -r /path/to/directory/*

Delete a directory in Linux using the find command

You can request the system to search for specific directories and delete them. For example, the following command finds all directories named dir and deletes them:

<pre>find . -type d -iname ‘dir’ –delete</pre>

  1. -Type D : Search within directories only.
  2. -name ‘dir’: Search for a directory named ‘dir’.
  3. -remove: Delete all found directories.

Find and move all empty directories in Linux.

You can use the find command to delete all empty sections at once. For example, the following line searches for a directory named dir and deletes all empty folders:

find . -type d -iname ‘dir’ -empty –delete
  1. -Type D : Search within directories only.
  2. -name ‘dir’: Search only in directories named ‘dir’.
  3. – empty : search for the empty directory.
  4. -remove: Delete all empty directories found.

What to do if permission is denied error message when deleting a directory?

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

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

If prompted, you must provide a root user or “sudo” user password.

Conclusion

Linux not only allows you to delete a directory but also allows you to search for empty directories with a specific name and delete them. You can also keep the directory, but delete all its contents. All this is possible using the above command. In this article, we have explained how to delete a directory in Linux. Also, we showed you how to delete files and subdirectories in Linux. Buy Linux VPS from Oudel Inc. If you want to know how to connect a Linux server with xRDP then you can read this article.

Scroll to Top