Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Find and check timestamps of files.

by Malach (Scribe)
on May 17, 2000 at 05:05 UTC ( #12022=perlcraft: print w/replies, xml ) Need Help??

   1: #!/usr/bin/perl
   2: 
   3: # Simple, but wonderfully useful.  Find all files on system with name
   4: # supplied on command line, and get a "long" listing of them that shows their
   5: # time stamp.
   6: #
   7: # next step, is to write one that sorts them into order of latest file alteration.
   8: 
   9: 
  10: foreach $_ (`locate $ARGV[0]`){
  11:         system ("ls -lt $_");}

Replies are listed 'Best First'.
RE: Find and check timestamps of files.
by turnstep (Parson) on May 17, 2000 at 07:03 UTC
    for $_ (`locate $ARGV[0]`) { chop; -M and $f{$_}=$^T-(3600*24*(-M _)); } for $x (sort { $f{$a} <=> $f{$b} } keys %f) { printf "%s $x\n", scalar localtime($f{$x}); }
    Ideally, you could even have perl grep through the "locate" database (locatedb) itself, and eliminate the system calls altogether.
      ewww... replace all that -M garbage with just (stat $_)[9]!
        Yeah...it was late when I wrote that. For prettiness, both could be used:
        -M and $f{$_}=(stat _)[9];

        Note: the initial test (the -M above) could also be almost any of the file tests - it is needed because locate returns a list of files that existed when the locate database was last compiled, but may not exist now. Also, the files may *exist*, but you may not be able to get information on them due to rights.

RE: Find and check timestamps of files.
by anders (Initiate) on May 26, 2000 at 22:04 UTC
    [anders@localhost anders]$ perl -e 'for (`locate $ARGV[0]`) {\ chomp; $h{$_} = (stat $_)[9] \ }\ foreach $k ( sort {$h{$a} <=> $h{$b} } keys %h) {\ print scalar localtime $h{$k}, ": $k\n" }' filename Thu Jul 29 11:20:32 1999: /usr/lib/xemacs/xemacs-packages/lisp/apel/fi +lename.elc Wed Nov 3 12:52:33 1999: /usr/bin/getfilename Wed Nov 3 12:52:34 1999: /usr/man/man1/getfilename.1.bz2 Mon Dec 20 08:47:16 1999: /usr/man/mann/filename.n Mon Jan 3 20:57:21 2000: /usr/share/zsh/functions/Commands/_correct_f +ilename
    -anders

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlcraft [id://12022]
Approved by root
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-01-18 17:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (213 votes). Check out past polls.

    Notices?