Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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.

Comment on How to find the most recent file?
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 (Canon) 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 (Prior) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://816530]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-07-12 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls