Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How to list files in dir with respect to time ?

by swaroop (Beadle)
on Aug 16, 2005 at 08:45 UTC ( [id://484100]=perlquestion: print w/replies, xml ) Need Help??

swaroop has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Help.

How to list the files in dir with respect to time ( similer to File::Listing::parse_dir(`ls -lrt $dir`). I personally do not like to use the UNIX native commands in script ( `ls –lrt ` ).

Please help me in finding the algorithm to sort the files with respect to time.

Thanks,
swaroop
  • Comment on How to list files in dir with respect to time ?

Replies are listed 'Best First'.
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/*";

    After Compline,
    Zaxo

      or even easier (and faster) with Sort::Key:
      use Sort::Key qw(nkeysort); my @files_by_mtime = nkeysort { (stat)[9] } glob "$dir/*";
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.


    Dave

Re: How to list files in dir with respect to time ?
by gellyfish (Monsignor) on Aug 16, 2005 at 09:01 UTC

    You are probably looking at something like this:

    #!/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\

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
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.

    One world, one people

      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 ... )


        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.

        One world, one people

Re: How to list files in dir with respect to time ?
by 5mi11er (Deacon) on Aug 16, 2005 at 14:31 UTC
      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
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

      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!

      Squibbie Pooh Ski Doo.

        Ahem, PRAGMATISM! doh.

        Squibbie Pooh Ski Doo.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://484100]
Approved by castaway
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-25 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found