Link to home
Start Free TrialLog in
Avatar of Talmash
TalmashFlag for Israel

asked on

find the date&hour of newest file/link under directory

hi .

$USER_ROOT is declared ,it's the root directory of my model .
I want to get out newest file or link located under that directory.

I need the data&time .

in c-shell  I can do it very easy .

I tried to run this code :

     $auto_link_1 = "ls -lt `find $ENV{USER_ROOT} -type f -name '*.v'` | head -1";
     @link_dot_v = system("$auto_link_1");
     print "runtst::Cheetah_Auto_Linking link_dot_v size = $#link_dot_v =\n@link_dot_v\n";

what I got is :

-rw-r--r--    1 talm     cheetah_    26447 Jan  8 21:34 /project/galileo102/cheetah/USERS/talm/final
_mfc/MODELS/Envir/mfc/stubs/ct_mfc_txq_stub.v
runtst::Cheetah_Auto_Linking link_dot_v size = 0 =
0

*** I can asume the first '[A-Z]' char in the "system" result is the Month .


ASKER CERTIFIED SOLUTION
Avatar of kandura
kandura

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here's something you can try to start with:

use File::Find;
use File::stat

my $latest;

sub eachfile { my $st = stat( $_) or return; # should warn here?
      my $mtime = $st->mtime;
      my $latest = [ $File::Find::dir, $_, $mtime] if $mtime > $latest->[2];
      }

find( $ENV{USER_ROOT});

print "most recent file is: ", $latest->[0], '/', $latest->[1], "\n";
Avatar of brokeMyLegBiking
brokeMyLegBiking

This creates a hash from the files in the specified directory, then sorts it, then prints out the first element of the sorted array.
--------------------------------------
$folder = $USER_ROOT;
@files = grep {-f} glob "$folder/*";
$fNT{$_} = sprintf "%010d", (stat $_)[9] for @files;#create hash with file and file last modified date

$newestFile = ( sort {$fNT{$a} <=> $fNT{$b}} keys %fNT )[0];#the first element of the sorted hash
print $newestFile;
--------------------------------------
my last script returned the oldest file, this one returns the newest/latest. ($b and $a are reversted in this version)
-------------------------
$folder = $USER_ROOT;
@files = grep {-f} glob "$folder/*";
$fNT{$_} = sprintf "%010d", (stat $_)[9] for @files;#create hash with file and file last modified date

$newestFile = ( sort {$fNT{$b} <=> $fNT{$a}} keys %fNT )[0];#the first element of the sorted hash
print $newestFile;
--------------------------
brokeMyLegBiking:
Your answer has a couple of minor drawbacks. It doesn't process the entire directory tree below the given path, which the OP wanted (which is why he used find).
Secondly, jmcg's suggestion is more efficient since it doesn't involve sorting, which could potentially be very time-consuming.

jmcg:
there are two typo's in your script ;^)

 use File::Stat;

 find( \&eachfile, $ENV{USER_ROOT} );

Talmash:
jmcg's script is the most efficient solution to your problem. Fix the typo's above and go with that script.

Kandura

good point.
Kandura,

Thanks for looking after me. I thought the S was lowercase in File::stat but I could be wrong. As for the other typos, I have no excuse, I should have waited for a time with fewer distractions before attempting to write a response.
ahem, yes File::stat is with a lower case...
That's the real reason I'm such a fan of incremental development: I never get it right the first time ;^)
I did get the feeling your post was unusually terse; I admire the consistent level of precision and concisiveness of your replies, and this one seemed a bit off.
Guess we all have our off days. :-)