[Gllug] quick question - exclusive rm -r

salsaman salsaman at xs4all.nl
Wed Nov 29 19:52:48 UTC 2006


Philip Hands wrote:

>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>Luis M. Cruz wrote:
>  
>
>>On 11/27/06, salsaman <salsaman at xs4all.nl> wrote:
>>    
>>
>>>Is it possible to say "delete all subdirectories of ., _except_ those
>>>containing the file foo" in one line of shell script ? If so, how ?
>>>      
>>>
>> Hi,
>>
>> something like this should work:
>>
>>for A in `find -type d`; do echo `find -type f -name foo` | grep -q $A
>>|| rm -fr $A ; done
>>    
>>
>
>The "echo `command` |" construct is an odd way of doing something similar
>to: "command |" with the added bonus that it'll break if the output of the
>command is too large.
>
>Also, this is liable to generate lot's of whining about the fact that
>directories found by the first find are liable to have been deleted by an
>early rm -rf, and so won't be there when you try to run the later find
>- -name bit as you descend the hierarchy.
>
>To deal with that, you can add the -depth option to the first find, so that
>it lists the directories from the deepest ones first, and so doesn't try to
>delete the top ones before checking the lower ones.
>
>Oh, and you can avoid repeatedly hunting the whole hierarchy for foos too.
>
>So, this is perhaps an improvement:
>
>  for A in $(find . -depth -type d) ; do [ -z "$(find $A -type f -name
>foo)" ] && rm -fr $A ; done
>
>  
>
Thanks, that worked a treat !

Gabriel.

-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list