Re: How to list files in dir with respect to time ?
by Zaxo (Archbishop) on Aug 16, 2005 at 08:58 UTC
|
You can get mtime as the tenth element of the list returned by stat. Here's a quick way to do it,
my @files_by_mtime =
map {$_->[0]}
sort {$a->[1] <=> $b->[1]}
map {[$_,(stat)[9]]}
glob "$dir/*";
| [reply] [d/l] |
|
use Sort::Key qw(nkeysort);
my @files_by_mtime = nkeysort { (stat)[9] } glob "$dir/*";
| [reply] [d/l] |
Re: How to list files in dir with respect to time ?
by davido (Cardinal) on Aug 16, 2005 at 08:56 UTC
|
You can use readdir to get the filenames in a directory, the -f test to filter out non-file entities, and then sort based on the -M test.
| [reply] |
Re: How to list files in dir with respect to time ?
by gellyfish (Monsignor) on Aug 16, 2005 at 09:01 UTC
|
#!/usr/bin/perl
+
use strict;
use warnings;
+
+
my $dir = '.';
+
opendir DIR, $dir or die "Can't open '$dir' - $!\n";
+
my @files = map { "$dir/$_"}
grep !/^\..?$/, readdir DIR;
+
my @sort_files = sort { $a->[1] <=> $b->[1] }
map { [ $_ , (stat($_))[9] ] }
@files;
+
+
foreach my $file ( @sort_files )
{
print $file->[0],"\t",scalar localtime($file->[1]),"\n";
}
/J\ | [reply] [d/l] |
Re: How to list files in dir with respect to time ?
by kprasanna_79 (Hermit) on Aug 16, 2005 at 11:14 UTC
|
Hi swaroop,
Please visit this link. I think u have forget the earlier replies for the same question.
-Prasanna.K | [reply] |
Re: How to list files in dir with respect to time ?
by anonymized user 468275 (Curate) on Aug 16, 2005 at 12:52 UTC
|
To crush it down to a one-liner, using glob seemed fastest the most apt: perl -e 'print "$_\n" for ( sort { -M $a <=> -M $b } glob "/path/\*" )
+;'
Update: by fastest I was thinking about coding effort rather than meaning to suggest a specific focus on performance.
| [reply] [d/l] |
|
You could use the Orcish Maneuver to make your code go faster...
# Original code
for ( sort { -M $a <=> -M $b } glob ... )
# Orcish Maneuver
my %t; for ( sort { ($t{$a} ||= -M $a)
<=>
($t{$b} ||= -M $b) } glob ... )
| [reply] [d/l] |
|
I've looked into this now - I was expecting sort to be smart enough to do that manouvre itself. For those who don't understand what it's doing, it is forcing the sort routine to perform the -M operation only once per file, but at the overhead of a hash lookup per sort iteration (there are geometrically upon n sort iterations per n files). sort + block could be optimised for minimal calculation of each side of the <=> operator (or cmp as appropriate) -- after all it ought to know it's going to be doing that irrespective of what's in the block, so it's a safe optimisation for sort to be doing already. But the orcish manouevre appears to be necessary because no such optimisation has been implemented. Perhaps Sort::Key has the manoeuvre already, however.
| [reply] |
|
Re: How to list files in dir with respect to time ?
by 5mi11er (Deacon) on Aug 16, 2005 at 14:31 UTC
|
| [reply] |
|
I'm new to Perl. In addition, I really struggled when I moved my scripts from Linux <=> Solaris.
We can write a simple code or algorithm, So Instead of using UNIX native commands (I mean OS specific commands or some modules, which supports specific OS).
Anyways, Thanks to everybody (: D
Swaroop
| [reply] |
Re: How to list files in dir with respect to time ?
by Anonymous Monk on Aug 16, 2005 at 14:09 UTC
|
I personally do not like to use the UNIX native commands in script.
That's a pity, since it means you can solve your problem in a single line - and you don't have to ask for a solution. It's also non-Perl, after all, Perl is a glue language.
The roll it your own solution involves using opendir, readdir, stat, sort and printf. You also need getpwnam and getgrnam if you want to similate the -l option of ls, and perhaps grep to filter out dot-files, although that can be done with other means as well. I hope that's enough answer for you, and you can work out the details - I won't bother with them, if
system "ls -lrt";
works just as well.
UNIX Monk | [reply] [d/l] |
|
Well, since Perl doesn't always run on Unix, and that having this dislike is a sign that this activity is likely more than just based on a purely aesthetic concern for the programmer (I guess that they want something that is capable of working interoperably on both win32 and Unixish perl, perhaps others);
I'd suspect that this is a better method to choose to maximize robustness, but if it's just going to run on something with accessible unix shells... I'd only do this to save forking costs, in that case...
Pramatism vs. Elegance!
| [reply] |
|
| [reply] |