en
  English
Phone :  +370 (5) 204-1903
Email: sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. How to delete directory in Linux

How to delete directory in Linux

In Linux, deleting directories can be seen as deleting files. It is one of the simple tasks in Linux that can be efficiently implemented by using the command-line interface or using the desktop file manager such as GNOME’s GUI. But, if you are working on a headless server and looking to delete some directories simultaneously, then it is better that you use the command-line interface. With the CLI, you can delete one or multiple directories with just a simple command.

Linux Tutorial Sep 21, 21 by Nisal N 6 min Read
How to delete directory in Linux

In Linux, deleting directories can be seen as deleting files. It is one of the simple tasks in Linux that can be efficiently implemented by using the command-line interface or using the desktop file manager such as GNOME’s GUI. But, if you are working on a headless server and looking to delete some directories simultaneously, then it is better that you use the command-line interface. With the CLI, you can delete one or multiple directories with just a simple command.

In this article, we will be majorly focusing on various commands and their options that will help you delete directories with great ease.

But before you proceed with deleting directories, make sure that you understand the basic concept. When you delete a directory through the desktop file manager, the directory will go to the trash or recycle bin. However, if you are using the command line to delete the directories, you have to be more careful. Once you run these commands, you will not be able to recover them.

In Linux, every task depends on what permission you have on a particular file or directory. For deleting a directory, you need to have the write permission on that directory to proceed with the deletion command. Else, you will get an “operation not permitted” error while running the command.

Removing Directories using rmdir command

For deleting the directory in Linux, you can use its “rmdir” utility for deleting the empty directories. So if you want to proceed with the empty directories, you can use the “rmdir” command to do so.

You can run the following “rmdir” command and the directory name you want to delete.

rmdir dir1

If the directory in the above command is not empty, you will get an error message. 

If you are looking to delete a directory that is not empty, then you need to use the “rm” command. Another option is to delete the directory’s contents and then delete the empty directory.

Removing Directories using rm command

For deleting non-empty directories, you can use the “rm” utility if the Linux system allows you to delete files and directories (the right permissions). You can use the “rm” command for deleting both empty and non-empty files and directories.

Using the “rm” command without providing any option will delete the files but not the directory. For deleting the directories, you need to mention the “-d” option with the “rm” command. Also, for deleting the non-empty directory, you need to use the “-r” option that specifies the recursive action.

You can run the following command for deleting the directory along with all its contents.

rm -r dir_name

If the directory has write-protected access and you want to delete that directory, then you will get a prompt asking for deletion confirmation. For removing the directory without getting the prompt, you can delete that directory forcefully using the “-f” option along with the “rm” command.

rm -rf dir_name

If you are looking to delete multiple directories simultaneously, you can run the rm command by providing directory names separated by spaces. 

rm -r dir_name1 dir_name2 dir_name3

If you want to confirm the deletion of each subdirectory within a specific directory, you can use the “-i” option to get the prompt to confirm. You can use the following command to do so.

rm -rI dir1

To remove the directory, type y and hit Enter.

rm: remove 1 argument recursively? y

For deleting directories having a specific keyword, you can use the regular expressions to match and delete multiple directories. Here we are considering the example where we delete the directories ending with bak with any number of starting characters and fixed characters just before “bak.”

rm -r *_bak

Sometimes, removing the directories using the regular expression may lead to deleting directories that are not meant to be deleted. So you can run the “ls” command on the current working directory to confirm if only the desired directories have been deleted.

Removing Directories using find command

Another option for deleting directories is to use the “find” command. This allows you to search for a specific file and directory and match them based on the given regular expression. Then, perform the mentioned task on those files and directories that match the regular expression.

You can consider the following example that will delete all the directories ending with the _cache in the current working directory.

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

Where,

  • -typed will specify that we are looking for the directories only.
  • -name ‘*_cache’ will specify only those directories that are ending in _cache.
  • -exec will specify that it will execute the command with the optional arguments.
  • {} + will specify that it will append the files to the end of the rm command to perform the deletion action.

Removing all empty directories

If you want to delete all the empty directories simultaneously, you can use the following command to do so.

find /dir -type d -empty -delete

It would be best if you used the “-delete” option with care, as it will delete everything after the starting point that you will mention. Consider this option to be the last one.

Conclusion

Linux offers you a flexible command, “rmdir”, that will help you delete the empty directories only. But if you are up to deleting a directory with some contents, you need to use the “rm” command that is suitable for deleting both the empty and non-empty directories. But it would be best if you mentioned some options like “-r” along with the “rm” command to delete the non-empty directory.

Apart from this, we have mentioned some other commands that can be used to delete directories using various options. You can go through this article to understand how multiple commands will work to delete a directory in Linux.

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