rm argument list too long find and xargsy

Chris F.A. Johnson chris-E7bvbYbpR6jSUeElwK9/Pw at public.gmane.org
Mon Apr 12 19:13:33 UTC 2010


On Mon, 12 Apr 2010, Colin McGregor wrote:

> On Mon, Apr 12, 2010 at 12:29 PM, teddymills <teddy-5sHjOODPK7E at public.gmane.org> wrote:
> >
> > I was trying to rm more than 1024 files at one time (old logs)
> > and rm returned a strange error. "Argument list too long"
> >
> > so find and xargs to the rescue..
> > find . -name 'myDEBUG*.log' | xargs rm
> >
> > http://en.wikipedia.org/wiki/Xargs
> > uname -a shows a kernel less than 2.6.23
> 
> Chris Johnson and Chris Browne have offered nice, neat, even elegant
> solutions that should be great... Let me offer the sledgehammer
> solution... Ugly, but it works :-) . Create a little script called say
> script :
> 
> -------------------------------------------
> 
> #! /bin/bash

    These days you're unlikely to find it a problem, but on some
    systems the shebang will not be effective if there is a space
    after #!

> echo "# /bin/bash" > script2

    Missing !

> find . -name 'myDEBUG*.log' | awk '{ print("rm " $1) }' >> script2

    That will also try to remove directories, and will fail if any
    filenames contain whitespace (propably only theoretical problems,
    but it never hurts to be safe); quote the filename and limit it to
    files:

find . -type f -name 'myDEBUG*.log' | awk '{ print "rm \"" $1 "\"" }' >> script2

    Or include directories in the rm command:

find . -name 'myDEBUG*.log' | awk '{ print "rm -rf \"" $1 "\"" }' >> script2

    Or do it without creating a new script:

find . -name 'myDEBUG*.log' | awk '{ print "rm -rf \"" $1 "\"" }' | sh

> chmod 700 script2
> 
> ./script2
> 
> --------------------------------------------
> 
> Okay, so I am a sucker for programs that write other programs, even in
> this trivial example :-) . Removing a bunch of old log files is a
> problem that may need doing, but is normally something that needs
> doing so infrequently that it doesn't need to be done well. In other
> words, better to do a quick, easy to come up with, ugly solution
> rather than spend a lot of time finding the most elegant solution :-)

   But it does need to be done correctly, and it should be done
   robustly!

-- 
   Chris F.A. Johnson, <http://cfajohnson.com>
   Author:
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list