1820553 Members
2381 Online
109626 Solutions
New Discussion юеВ

Re: Perl with find

 
SOLVED
Go to solution
Coolmar
Esteemed Contributor

Perl with find

Hi,

I found this one-liner on IRTC that James Ferguson came up with and it is very close to what I want. I am trying to decipher it and learn perl but am short on time right now and need a solution. It finds all files in a directory structure that have changed in the last 30 minutes. I need it to print out the files (including datestamps like ls -l would) that have been created or modified in the last 24 hours.

Thanks,


perl -MFile::Find -le 'find(sub{print $File::Find::name if -f $_ && -M _ < 1/48},".")'
7 REPLIES 7
spex
Honored Contributor

Re: Perl with find

Hi Coolmar,

Why not just use the 'find' command?

$ find /path -type f -mtime -1 -exec ls -l {} \;

But if you are set on using that perl one-liner, change '1/48' to '1' for the last 24 hours.

PCS
Coolmar
Esteemed Contributor

Re: Perl with find

Yeah, I was able to figure out the 1/48 to 1 but how do I print the output in ls -l format?
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl with find

Hi:

While I would agree with Spex for cases not involving fractional days, you could amend my snippet to something as simple as:

# perl -MFile::Find -le 'find(sub{print join " ",$File::Find::name,scalar localtime((stat($_))[9]) if -f $_ && -M _ <= 1},".")'

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Perl with find

Hi (again):

Actually it looks better if we print the timestamp first:

# perl -MFile::Find -le 'find(sub{print join " ",scalar localtime((stat($_))[9]),$File::Find::name if -f $_ && -M _ <= 1},".")'

Regards!

...JRF...
Coolmar
Esteemed Contributor

Re: Perl with find

Thanks James! That's perfect.
A. Clay Stephenson
Acclaimed Contributor

Re: Perl with find

The key to what you are trying to do is the stat() function which returns file mode, uid, gid, size, times, number of links, ,,, . The times returned by stat() are in epoch seconds so that you need localtime() or strftime() to format them into more readable time values. You also need getpwuid() and getpwgid() to translate the UID's and GID's. Essentially, you are going to replace the sub(print) with a function that does the printing.

Do a man perlfuncs and a man File::Find and you should be in business.

It really does you no favors to fully code the solution for you but you have enough pieces to put it together.
If it ain't broke, I can fix that.
Coolmar
Esteemed Contributor

Re: Perl with find

Thanks Clay...that gives me alot to start with.