tarballed
July 2nd, 2003, 20:43
Just a quick Q here.

What is the best and easiest way to remove certain directories and leave other directories alone?

For instance, if i have the directory /tarballed and it has sub directories of:

elmore :)
bear
cat
dog

and I wanted to remove all the directories except elmore, what would be the best way to go about doing that?

Any suggestions?

Tarballed

bsdjunkie
July 2nd, 2003, 21:36
cd /tarballed
rm -rf bear cat dog

:P

tarballed
July 2nd, 2003, 21:39
LOL!!

What I meant was..hehe, lol...if I have 50+ directories in there and I want to remove lets say about 35 of them...

I can do that one by one, but there must be a easier way, yes?

hehe

I like that one thought bsdjunkie..

Tarballed

frisco
July 2nd, 2003, 21:44
[code:1:af50099f9c]
ls tarballed | grep -v ^elmore$ | xargs rm -rf

#to avoid more than one dir
ls tarballed | egrep -v '^(elmore|frisco)$' | xargs rm -rf
[/code:1:af50099f9c]

That won't work if one of the dirs has a space in it. You'd need to use find and some fancier footwork to avoid spaces:

[code:1:af50099f9c]
find tarballed \! -name tarballed -a \! -name elmore -print0 | xargs -0 rm -rf

#string on '-a \! -name $DIR' to avoid more
find tarballed \! -name tarballed -a \! -name elmore -a \! -name frisco -print0 | xargs -0 rm -rf
[/code:1:af50099f9c]

Be careful with those find's. Adding a / to the end of tarballed can change everything.

frisco
July 2nd, 2003, 21:53
What I meant was..hehe, lol...if I have 50+ directories in there and I want to remove lets say about 35 of them...

You need to find something in common between the dirs you want to delete and those you want to keep. You have all the unix utilities to do this. Think in terms of date, size, filenames, number of files, etc etc. Hopefully you can find something in common between the dirs you need to delete.

If you can't, then do something like:
[code:1:98acdc892c]
find /tarballed > /tmp/dirlist
vi /tmp/dirlist #delete the dirs you don't want rm'd
cat /tmp/dirlist | xargs rm -rf #same problems with spaces exists
[/code:1:98acdc892c]