procmail
[Top] [All Lists]

Re: ls Error

2000-05-18 07:16:48
On May 15,  4:48am, SoloCDM wrote:
Subject: ls Error
Recently I executed the following command:

     rm -f dummy "ls -t msg.* | sed -e 1,900d"

I don't know the quoting convention for bash, but in csh, the
line above should read:
       rm -f dummy `ls -t msg.* | sed -e '1,900d'`

Also, if you're trying to remove the _first_ 900 messages, I
think you'll need to add the -r option to ls above:
       rm -f dummy `ls -tr msg.* | sed -e '1,900d'`

Otherwise you'll be deleting the most recent 100 messages.
In the examples below, I added -r.


As a result, I received the following error:

     bash: /bin/ls: Argument list too long

I know why (14,522 msg.* files exist in the directory), but I need a
solution.

The general solution to this problem is tricky.  Off-hand, you'd
want to use:

(WARNING: don't try this out on your only copy of the mail
archive directory!  Make sure that you work on a copy until
you have the bugs ironed out.)

find . -name 'msg.*' -print | xargs ls -1tr \
   | sed -e '1,900d' | xargs rm -f

but the problem is that the first call to xargs will do the 'ls -1t'
in batches of 20K bytes or so (cf, the xargs manpage).  You can work
around this, to a limit, by specifying a big upper limit on
the size of the arg. vector (in csh):

set size = `find . -name 'msg.*' -print | wc -c`
# make it bigger, just to be sure, and in case some more files
# were created by the time we get around to deleting them
@ size = 2 * $size
find . -name 'msg.*' -print | xargs -s $size ls -1tr | xargs rm -f

Above, if xargs can't allocate 'size' bytes, at least it
will abort before doing the 'rm' step.

More complicated, but not subject to the vaguaries of xargs
limitations:

find . -name 'msg.*' -print | xargs ls -1l --full-time \
   | sort +9n -10 +6M -7 +7n -8 +8 -9 \
   | sed -e '1,900d' | awk '{print $NF}' | xargs rm -f

Above, the --full-time option is required, and is supported only
with GNU ls (available as the default on Linux).  I think the
sort command is correct, you'll need to verify that it is picking
off the year, month, day, time fields at the correct places.

<Prev in Thread] Current Thread [Next in Thread>