From owner-freebsd-questions@FreeBSD.ORG Mon Nov 7 19:43:25 2005 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id BD79B16A41F for ; Mon, 7 Nov 2005 19:43:25 +0000 (GMT) (envelope-from jellis@dhnet.us) Received: from smtp1.linkline.com (smtp1.linkline.com [66.59.235.27]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4080143D49 for ; Mon, 7 Nov 2005 19:43:24 +0000 (GMT) (envelope-from jellis@dhnet.us) Received: from [64.30.211.58] (64-30-211-58.dsl.linkline.com [64.30.211.58]) by smtp1.linkline.com (Postfix) with ESMTP id 035F89D0BA; Mon, 7 Nov 2005 11:43:21 -0800 (PST) User-Agent: Microsoft-Entourage/11.2.0.050811 Date: Mon, 07 Nov 2005 11:43:23 -0800 From: Jeffrey Ellis To: David Fleck , Jeffrey Ellis Message-ID: Thread-Topic: How to sort find results Thread-Index: AcXj04NGwYvh1k/GEdqYJgAKlXMBfA== In-Reply-To: <20051107132817.L3084@grond.sourballs.org> Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit Cc: FreeBSD questions Subject: Re: How to sort find results X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Nov 2005 19:43:25 -0000 Hi, David-- Thank you. Wow. That looks great... Um... Can you tell me how to run it? All My Best, Jeffrey on 11/7/05 11:32 AM, David Fleck at david.fleck@mchsi.com wrote: > On Mon, 7 Nov 2005, Jeffrey Ellis wrote: >> Well, at least I know it can do it now. The problem -- as usual for a newbie >> -- is that I haven't got the vaguest understanding of what I just read. The >> field part I think I get, but how would I use the first character? I guess >> I'm basically too stupid to get these kind of instructions -- maybe just one >> example for the use of each option included in man pages would help? > > Here's a completely different approach. I ran into this exact problem > often enough that I wrote a small Perl script to handle it: > > #!/usr/bin/perl > > use strict; > use File::Find (); > > # for the convenience of &wanted calls, including -eval statements: > use vars qw/*name/; > *name = *File::Find::name; > > (@ARGV) or usage(); > > my (%files, $tString, $reverse); > > $reverse = 1 if ($ARGV[1] =~ /r.*/); > > # Traverse desired filesystems > File::Find::find(\&wanted, $ARGV[0]); > > # sort %files by mod. time, print. > if ($ARGV[1] =~ /r.*/) { > foreach my $f (sort { $files{$b} <=> $files{$a} } keys %files) { > # chop off day of week > ($tString = scalar localtime($files{$f})) =~ s/\w* //; > print $tString, "\t",$f,"\n"; > } > } else { > foreach my $f (sort { $files{$a} <=> $files{$b} } keys %files) { > # chop off day of week > ($tString = scalar localtime($files{$f})) =~ s/\w* //; > print $tString, "\t",$f,"\n"; > } > } > exit; > > > sub wanted { > my (@fstat); > # put the filename and mod. time into %files > ((@fstat) = lstat($_)) && -f _ && ($files{$name} = $fstat[9]); > } > > sub usage { > print "\n", > "Usage: $0 (directory) [reverse]\n", > " examines all files in (directory) and all its subdirectories,\n", > " sorts by date, and returns the sorted list, earliest first.\n", > " If 'reverse' is specified, files are sorted earliest last.\n\n"; > exit; > } > > > -- > David Fleck > david.fleck@mchsi.com >