General
cancel
Showing results for 
Search instead for 
Did you mean: 

Re: script to get the most recent updated file in a dir-tree

 
SOLVED
Go to solution
James R. Ferguson
Acclaimed Contributor

Re: script to get the most recent updated file in a dir-tree

Hi Peter:

While I didn't use perl's Find module, but rather called the system's 'find' to begin the work, the approach we both took is similar.

I've attached a better version of what I first wrote. It's not biased -- it reports all files with the same, most recent timestamp.

See the attached!

Run as:

# ./mfile dirname

You can specify multiple directories as arguments, too.

Regards!

...JRF...
Hoefnix
Honored Contributor

Re: script to get the most recent updated file in a dir-tree

JRF,

This looks great. I am already using your first script because that fits already my requirements. I am extending it with other checks using the output of the stat function in an array. This saves me running trough the tree structure multiple times, which I should have done without a perl solution.
(I needed to check also the data on permissions/group ownerships etc.., that is now easy with this perl script.)

Many thanks for your help in putting me in the right direction.

I leave this thread open til Monday to see if anyone can think of some other solution. It's always nice to see how many people can come up with different solutions with the same result.

Peter
Ralph Grothe
Honored Contributor

Re: script to get the most recent updated file in a dir-tree

Peter,

that your Perl compiler complained about undeclared variables $latest and $file,
and moaned about an undefined sub "our"
indicates to me that you run it with a very dated Perl installation.
Actually, the our type was introduced with Perl 5.6 to replace the somewhat awkward "use vars" pragma.
I suggest you update your Perl
(if that is possible for you).
What is your Perl version?
(run "perl -v", or "perl -V")
Had I included the line

require '5.006';

then your Perl compiler should have exitetd with a meaningful message.

Besides, it was silly of me to declare the variables as our.
Just substitue the "our" with "my",
and it should execute with your Perl.

Or in fact you could get rid of any variable declaration at all, and remove the line with the "use strict" pragma.
For such a tiny bit of code its perfectly in order to have all variables implicitly declared as package globals (same as prepending an "our" at declaration with a Perl >= 5.6)
This is what the compiler silently assumes.
But for any Perl code extending a hundred lines or using different namespaces (viz. packages/modules) it should be mandatory to "use strict".

Apropos, if you strip off the -f test
then every kind of file (in the Unix sense) will be taken into account.
Madness, thy name is system administration
Hoefnix
Honored Contributor

Re: script to get the most recent updated file in a dir-tree

All,

Thanks for the instructive input you all gave me.

Cheers,
Peter