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

Delete a directory in linux
By

0 Comment

3 mins Read

Delete a directory in linux

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.

There are two commands to delete a file in Linux including the “rmdircommand and “rmcommand. Here we explain the most popular possibilities for these commands.

Linux Hosting Simplified Linux Hosting Simplified

Want a better way to host your websites and web apps? Developing something new? Simply don’t like Windows? That’s why we have Linux VPS.

Get your Linux VPS

How to Delete an Empty Directory in Linux?

delete a directory in linux

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

rmdir

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

Also Read: Connect to Linux server with XRDP

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.

Also Read: List Running Services on Linux

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 Hosting Simplified Linux Hosting Simplified

Want a better way to host your websites and web apps? Developing something new? Simply don’t like Windows? That’s why we have Linux VPS.

Get your Linux VPS

Conclusion

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.

A Web designer, with Experience in digital marketing , Totally fueled by passion to understand and learn different aspects of digital marketing.

Comments

Leave a Comment

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


Latest Posts