[NTLUG:Discuss] rm doesn't recurse

Kenneth Loafman ken at lt.com
Fri May 7 14:57:10 CDT 2004


Steve Baker wrote:

> Kenneth Loafman wrote:
> 
>> The method to avoid spawning thousands of rm's is to use find as:
>>
>> find . -type d -exec rm *.zip \;
>>
>> with the appropriate quoting, which I can never remember.
> 
> 
> That *STILL* doesn't work - and it's really important that
> you grok the reason why it doesn't.
> 
> Chant this mantra thrice daily:
> 
>   UNDER UNIX/LINUX, WILDCARDS ARE EXPANDED BY THE SHELL
>   BEFORE THE UNDERLYING COMMAND IS RUN.
> 
> So - the '*.zip' is expanded by the shell BEFORE find is
> even loaded into memory.   The shell has no idea that
> this argument is going to be used when find is down in
> some sub-directory.   So it'll expand ONLY those zip files
> that happen to be in the current directory.
> 
> This is *SUCH* an important thing to understand.
> 
> Failure to understand that will cause you endless grief
> in the future when commands either fail or wipe out
> your system!

Been doing UNIX since the 70's.  You might actually think about reading the 
message before lecturing.  Note that I said "with the approptiate quoting" 
in my message.  For your information the command:

find . -type d -exec sh -c "rm -f {}/*.zip" \;

works as it should.  It saves thousands of rm spawns for large cleanups. 
There is a way to escape it to execute in find without the sh, but its 
horribly obtuse and I just can't remember how to do it.  We'll leave that as 
an exercise for the reader.  I just don't have the time today.

...Ken





More information about the Discuss mailing list