[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Alternatives to ls for sorting files by modification time



On Wed, 2002-09-25 at 10:07, Holger Rauch wrote:
> Hi Vineet!
> 
> Thanks a lot for your quick reply!
> 
> On Wed, 25 Sep 2002, Vineet Kumar wrote:
> 
> > [...]
> > processing the list, i.e. with xargs?  It's hard to make suggestions
> > without seeing what you're trying to do...
> 
> You're right ;-) What I'm doing is
> 
> FILES=`$LS -lt1 $BACKUP_DIR/arc/*.arc | $TAIL -$NUM_OF_FILES`
> for i in $FILES; do
>   $RM -f $i
> done

> I want to remove the oldest $NUM_OF_FILES files and it seems to me that
> piping into tail fails when ls returns too many files.

The failure occures not because of the pipe, but because
$BACKUP_DIR/arc/*.arc is expanded to too many files by the shell.

and, as Vineet said: xargs is great! But it won't solve the problem
here. Probably you want:

find "$BACKUP_DIR/arc" -name '*.arc' -printf '%A@:%p\n' \
	| sort -g  -t : \
	| cut -d : -f 2- \
	| head -n "$NUM_OF_FILES" \
	| xargs -r rm -f

CAREFUL: this is untested!

I'd rather use 0-terminated records, but cut and head don't grok these,
apparently. So filenames must not have newlines (not a bad requirement,
I'd hope).

If somebody wants to do some clever scripting: I have a similar need,
but not yet found a simple solution: I want to purge some cache
directory and just leave the most recently accessed k megabytes. File
sizes vary greatly, so file count won't help much.

(btw: where in testing is textutils.info page? textutils 2.1-1 doesn't
contain it. Will this reappear with coreutils?)

-- 
secure email with gpg                           http://fortytwo.ch/gpg

NOTICE: subkey signature! request key 92082481 from keyserver.kjsl.com

Attachment: signature.asc
Description: This is a digitally signed message part


Reply to: