#!/usr/bin/perl -w # Copyright © 2001 by Julian Fong . All rights reserved. # # Permission to use, copy, modify, distribute, and sell this software # and its documentation for any purpose is hereby granted without fee, # provided that the above copyright notice appear in all copies and # that both that copyright notice and this permission notice appear in # supporting documentation. No representations are made about the # suitability of this software for any purpose. It is provided "as # is" without express or implied warranty. # # newfile: Lists the newest file in the directory. Optional argument # -n specifies the nth newest file use strict; use File::stat; my ($index, $dir, %files, @sorted); $index = 0; $dir = "."; while ($ARGV = shift) { if ($ARGV =~ /^-(\d+)/i) { $index = $1; } else { $dir = $ARGV; } } opendir(DIR, $dir) || die ("Can't open directory $dir: $!\n"); %files = map { stat("$dir/$_")->mtime => "$dir/$_" } grep (-f "$dir/$_", readdir(DIR)); unless ($index < scalar keys %files) { die ("Can't access file with index $index, must be less than " . (scalar keys %files) . "\n") } @sorted = sort {$b <=> $a} (keys(%files)); print "$files{$sorted[$index]}\n";