Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

How to find the most recent file?

by zli034 (Monk)
on Jan 09, 2010 at 19:31 UTC ( #816530=perlquestion: print w/replies, xml ) Need Help??
zli034 has asked for the wisdom of the Perl Monks concerning the following question:

There are millions of files in my PC. How can I find the most recent created file or modified in the directory system? Do I need some scripts to do this? Sounds complicate, or there is a simple PC trick to do the job? I really wanna know. Thanks in advance.

Replies are listed 'Best First'.
Re: How to find the most recent file?
by almut (Canon) on Jan 09, 2010 at 20:14 UTC

    Using find would be one way.  For example, to find (print) all files in the file system that have been modified/created in the last three days:

    $ find / -mtime -2

    Update: actually, rereading your question I see you wrote file (singular), whereas I had read files ...  So, to get the one most recent file only, it would rather be something like

    $ find / -mtime -2 -type f -exec ls -l {} \; | sort -k6 | tail -1
Re: How to find the most recent file?
by shmem (Chancellor) on Jan 09, 2010 at 20:15 UTC

    Here's a simple trick, though perl, not PC:

    #!/usr/bin/perl use File::Find; use Getopt::Std; use Cwd; use strict; my $u = "usage: $0 -n count -s skip-pattern dir [dir ...]\n" . " where count is number of newest files to report\n" . " and skip-pattern is a comma separated list of regexes\n" . " to apply upon files found, which will be - skipped\n"; die $u unless @ARGV; my %o; getopt('ns',\%o); my $p; my $s; if($o{s}) { ($p=$o{s}) =~ s/,/\|/g; $s++; } my $n = $o{n} ? $o{n} - 1 : 10; # default 10 files my @d = @ARGV; push @d, getcwd unless @d; my @f; $#f = $n; # preallocate $n elements ! -d $_ and warn "directory $_: $!\n" and undef $_ for @d; @d = grep { defined $_ } @d; @d or die "no searchable directories in argument list\n"; find(\&wanted, @d); print "skip-pattern: ($p)\n" if $o{s}; print join("\n", map { scalar(localtime $_->[0]).' '.$_->[1] } @f ),"\n"; sub wanted { my $file = $File::Find::name; -d && return; $s and $file =~/($p)/ and return; my $time = (stat)[9] or die "can't stat $file: $!\n"; # mtime # if file is newer or as new than the first file... if ($f[0]->[0] < $time) { unshift @f,[$time,$file]; pop @f if $#f > $n; return; } # ...else insert the found file in the list. for( my $i = 0; $i<= $#f; $i++) { if($time >= $f[$i]->[0]) { die unless $time; splice @f,$i,0,[$time, $file]; pop @f if $#f > $n; return; } } }

    update: improved code

Re: How to find the most recent file?
by Ratazong (Monsignor) on Jan 10, 2010 at 08:07 UTC

    you might want to try the following algorithm:

    1. assume the newest file was created long ago (1.1.2000) and store that value to a variable $newest_date
    2. pass through all files - using File::Find on your root directory (e.g. C://)
    3. in the callback-funtion check whether the creation-date of the current file is newer than the newest found before ... if yes, update $newest_date and store the new filename

    You might want to read the documentataion of File::Find before, e.g. how to get the last-modifued date of the current file ...

    HTH, Rata

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://816530]
Approved by planetscape
[choroba]: three
[choroba]: I mean I haven't used DBIx::Class nor M:S, only Moo
[1nickt]: Haha, I was learning about Linux filesystem trees trying to understand how Moo fits.
[1nickt]: I just got my feet wet with some DBIC coding this last week. I feel "meh" about it so far. It was pretty easy to add a sub to the DBIC class to filter search results ...
[1nickt]: ... in the calling code ... $rs = $rs->maturity(" stable");. But I don;t enjoy feeling that far away from the SQL and the DB, really.
[1nickt]: The same project (CPAN Testers API) is using experimental signatures (and therefore 5.024) ... which is what replaced Method::Signatures , afaict.
[1nickt]: But the new $job is on an older Perl and therefore using M::S, the syntax of which looks clunky to me, so far.

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2017-11-24 13:31 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (349 votes). Check out past polls.