From owner-freebsd-questions@FreeBSD.ORG Mon Nov 7 19:32:18 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 6A0C816A41F for ; Mon, 7 Nov 2005 19:32:18 +0000 (GMT) (envelope-from david.fleck@mchsi.com) Received: from sccmmhc92.asp.att.net (sccmmhc92.asp.att.net [204.127.203.212]) by mx1.FreeBSD.org (Postfix) with ESMTP id ED0BC43D48 for ; Mon, 7 Nov 2005 19:32:17 +0000 (GMT) (envelope-from david.fleck@mchsi.com) Received: from grond (12-216-7-29.client.mchsi.com[12.216.7.29]) by sccmmhc92.asp.att.net (sccmmhc92) with SMTP id <20051107193216m920066jute>; Mon, 7 Nov 2005 19:32:17 +0000 Date: Mon, 7 Nov 2005 13:32:16 -0600 (CST) From: David Fleck Sender: dcf@grond.sourballs.org To: Jeffrey Ellis In-Reply-To: Message-ID: <20051107132817.L3084@grond.sourballs.org> References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed 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:32:18 -0000 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