Thursday, February 17, 2011

Bash delete directories except that one

Ran into a scenario where I needed to delete all subdirectories of target except target/keepme/. So I want rm -rf target/subdir1 target/subdir2 ... target/subdirN with one directory omitted. I want to use grep to remove the directory I want to keep from the list so something like this should work:


  1. Print the list of directories echo target/*/
  2. Flip space separator to newline  tr " " "\n"
  3. Remove the line for the directory to keep grep -v keepme
  4. Flip newline back to space  tr "\n" " "
  5. Run rm -rf on the results from steps 1-4

As a 1-liner:

rm -rf $(echo target/*/ | tr " " "\n" | grep -v keepme | tr "\n" " ")

Noting it in case I need to do it again someday ;)

15 comments:

Kamran Zafar said...

How about this one:

find target/* ! -iname keepme -print0 | xargs -0 rm -rf

this works for me

navya said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

Mounika said...

A universal message I suppose, not giving up is the formula for success I think. Some things take longer than others to accomplish, so people must understand that they should have their eyes on the goal, and that should keep them motivated to see it out til the end.
python Training institute in Pune
python Training institute in Chennai
python Training institute in Bangalore

sheela said...

Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
Best Devops online Training
Online DevOps Certification Course - Gangboard

priya said...

It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command
Data Science training in Chennai
Data science training in Bangalore
Data science training in pune
Data science online training
Data Science Interview questions and answers
Data Science Tutorial
Data science training in bangalore

offpageseo70 said...

Nice blog,I understood the topic very clearly,And want to study more like this.
Data Scientist Course

Avijit said...

There are numerous parts of this article on which I agree with you. You have created neural connections in my mind not utilized frequently. Much thanks to you for getting my neurons bouncing.


SEO services in kolkata
Best SEO services in kolkata
SEO company in kolkata
Best SEO company in kolkata
Top SEO company in kolkata
Top SEO services in kolkata
SEO services in India
SEO copmany in India

eazyclasses said...

I need to concur with the admirable statements you make in your article since I see things like you. Moreover, your substance is fascinating and great understanding material. Much obliged to you for sharing your ability.


Online Teaching Platforms
Online Live Class Platform
Online Classroom Platforms
Online Training Platforms
Online Class Software
Virtual Classroom Software
Online Classroom Software
Learning Management System
Learning Management System for Schools
Learning Management System for Colleges
Learning Management System for Universities

BestTrainingKolkata said...

Indeed a great article! I am definitely going to bookmark it to go through it over again after work. It seems to cover the major information about the topic in points.
SAP training in Mumbai
SAP course in Mumbai
SAP training institute Mumbai

BestTrainingMumbai said...

This content is very considered and well-thought. You are a distinctive thinker. You have put up the unique points to bring up specific points. Please continue doing such good work.
SAP training in Kolkata
SAP course in kolkata
SAP training institute in Kolkata

EXCELR said...

Thanks for sharing great information. I like your blog and highly recommend. We also offer best data science training in Hyderabaddata scientist courses

hager ali said...

شركة نقل عفش بجدة
نقل عفش بجدة
رخيص نقل عفش بجدة
شركة نقل عفش
نقل عفش بجدة
ارخص شركة نقل عفش بجدة
افضل شركة نقل عفش بجدة
افضل شركة نقل عفش بمكة
شركة نقل عفش بمكة
ارخص شركة نقل عفش بمكة
نقل عفش مكة
نقل عفش مكه رخيص

for ict 99 said...

Great Article android based projects

Java Training in Chennai

Project Center in Chennai

Java Training in Chennai

projects for cse

The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

hrithiksai said...

Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data sciecne course in hyderabad

Mohd Sharique said...

Wow! Such an amazing and helpful post this is. I really really love it. I hope that you continue to do your work like this in the future also.

Python Training Institute in Pune
Best Python Classes in Pune

Post a Comment