If your daily work involves doing stuff on the Linux command line, it's reasonable to assume you get to learn a lot of new things every now and then. That's because many a times tasks are not straight forward, and only those with good knowledge of command line tools can perform them successfully.

For example, consider this: suppose you have multiple project directories (proj1, proj2, proj3, ...) in your home directory, and each of them contains a directory named 'temp'. Now, the task is to remove all these 'temp' directories in one go. What would you do?

If you are a command line newbie, it's worth knowing that you can perform this operation with a single command, and the tools that'll let you do this are find and rm. Let's see how.

The following screenshot shows the 'temp' directories in various project directories:

find-dir-recursive

So here's the find command that you need to execute in this case:

find /home/himanshu/projects -maxdepth 2 -type d -name temp -exec rm -r {} +

Following the output of the dir command after the aforementioned command was executed:

find-dir-again

So that confirms all 'temp' directories were successfully deleted. While information on options like maxdepth, type, name, and exec is there in the find command' man page, you can learn about them through examples by heading here.

Linux: How to remove subdirectories with same name but residing in different directories

Leave a Reply

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

*

Ad #native_company# — #native_desc# #native_cta#