Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

RE: Find and check timestamps of files.

by turnstep (Parson)
on May 17, 2000 at 07:03 UTC ( [id://12030]=note: print w/replies, xml ) Need Help??


in reply to Find and check timestamps of files.

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.

Replies are listed 'Best First'.
RE: RE: Find and check timestamps of files.
by merlyn (Sage) on May 17, 2000 at 20:06 UTC
    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.

        OK, that's a hell of a lot nicer than my initial hack....

        Now, all I have to do is figure out what the hell it all means :)

        for (`locate $ARGV[0]`) { chop; -M and $f{$_}=($stat _)[9]; } for $x (sort { $f{$a} <=> $f{$b} } keys %f) { printf "%s $x\n", scalar localtime($f{$x}); }
        Broken down:
        for $y (`locate $ARGV[0]`) { ## Run the system command locate, and send it the first ## argument that was sent to the script. This will ## return a list of lines (files), which gets stored into $_ ## (because we did not give the for loop a variable) chop; ## We need to chop the newline off for the following ## tests to work. With no argument, it chops $_ -M and $f{$_}=(stat _)[9]; ## -M returns the last modification time. With no argument, ## it checks $_. We do this because locate may not return ## a valid file. Perl actually does a stat behind the ## scenes to get this info, and saves it away. We use the ## 'and' to only do the next part of the statement if ## -M returns something. ## (stat _)[9] - the underscore tells perl not to check ## the file again, but to use the same information it ## saved when it made the last check (which was -M) ## Stat returns a list, but we only want the 10th ## element (in position 9, count from 0) which is the ## modification date (similar to -M). We put this ## time into the hash %f, using the filename (which is ## still set as $_ from before) as a key. } ## That's it, now the next line of locate's output is ## parsed, until we are done. for $x (sort { $f{$a} <=> $f{$b} } keys %f) { ## This says to sort all the keys of the hash %f, ## which are the names of the file. We use a custom ## sort by comparing the values of the hash, which ## are the modification times. $a and $b are special ## variables perl uses inside sort blocks, and the ## spaceship operator <=> allows us to compare ## the times numerically. This sorted list is then ## passed to the for loop, with $x receiving the ## keys of %f in the newly sorted order. printf "%s $x\n", scalar localtime($f{$x}); ## We print it out. No particular reason to use printf - ## I just happen to like it. The scalar and localtime ## converts the value stored in $f{$x} (the modification ## time) into something prettier to read. (the time is ## stored internally as a 32-bit number, i.e. 958740893 ## which is not very pretty at all.) }
        Hope that helped. I know that this is probably really basic to most people (even the original poster) but somebody may get some use out of my verbosity. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://12030]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found