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

How to Remove directory in Linux

How to remove directory in Linux? In Linux, removing directories can be seen as deleting files. It is one of the simple tasks in Linux that can be efficiently implemented using the command-line interface or 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 Remove directory in Linux

List of content you will read in this article:

How to remove directory in Linux? In Linux, removing directories can be seen as deleting files. It is one of the simple tasks in Linux that can be efficiently implemented using the command-line interface or 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 directory in Linux. 

But before you proceed with deleting folders in Linux, make sure that you understand the basic concept. When you delete folder in Linux 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. To remove folder in Linux, 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 in Linux

For deleting the directory, you can use the rmdir Linux utility to delete 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. So how to rmdir directory not empty?

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 the 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 proper permissions). You can use the rm command in Linux to remove directory and contents.  

Using the rm command without providing any option will remove all files in a directory Linux but 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 to delete the directory and 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. To remove directory without getting the prompt, you can forcefully delete that directory 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 directory 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 the 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 end 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 remove the empty directories only. But if you want to remove directory in Linux 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.  You can also buy linux server to practice it on a routine basis.

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