Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to check last modify date

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
Looking to write a script, but not sure how to get started. I am a complete noob to perl so bear with me.

I need to write a script that will loop through subdirectories, and load an array with the directory name, last modified date, and if modified within the last 12 hours flag with a Y else flag with an N.


All help is greatly appreciated to get me started on this.
 
Here's a starter for you:

Code:
[gray]#!/usr/bin/perl[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]File::Find[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@dirs[/blue] = [red]([/red][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]$now[/blue]  = [url=http://perldoc.perl.org/functions/time.html][black][b]time[/b][/black][/url][red];[/red]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]process[/maroon] [red]{[/red]
  [url=http://perldoc.perl.org/functions/return.html][black][b]return[/b][/black][/url] [olive][b]unless[/b][/olive] [url=http://perldoc.perl.org/functions/-X.html][black][b]-d[/b][/black][/url] [blue]$_[/blue][red];[/red]

  [black][b]my[/b][/black] [blue]$lmtimedate[/blue] = [red]([/red][url=http://perldoc.perl.org/functions/stat.html][black][b]stat[/b][/black][/url] [blue]$_[/blue][red])[/red][red][[/red][fuchsia]9[/fuchsia][red]][/red][red];[/red]
  [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@dirs[/blue], [red]{[/red] [purple]name[/purple] => [blue]$_[/blue], [purple]lmtimedate[/purple] => [url=http://perldoc.perl.org/functions/scalar.html][black][b]scalar[/b][/black][/url][red]([/red][url=http://perldoc.perl.org/functions/localtime.html][black][b]localtime[/b][/black][/url][red]([/red][blue]$lmtimedate[/blue][red])[/red][red])[/red], [purple]mod12[/purple] => [red]([/red][blue]$now[/blue] - [blue]$lmtimedate[/blue] <= [fuchsia]12[/fuchsia][blue]*[/blue][fuchsia]60[/fuchsia][blue]*[/blue][fuchsia]60[/fuchsia] ? [red]'[/red][purple]Y[/purple][red]'[/red] : [red]'[/red][purple]N[/purple][red]'[/red][red])[/red][red]}[/red][red];[/red]
[red]}[/red]

[maroon]find[/maroon][red]([/red][red]{[/red] [purple]wanted[/purple] => \[maroon]&process[/maroon], [purple]follow[/purple] => [fuchsia]1[/fuchsia] [red]}[/red], [red]'[/red][purple].[/purple][red]'[/red][red])[/red][red];[/red]

[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$dir[/blue] [red]([/red][blue]@dirs[/blue][red])[/red] [red]{[/red]
  [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Dir: [blue]$dir[/blue]->{name}    Last Modified: [blue]$dir[/blue]->{lmtimedate}    Recently: [blue]$dir[/blue]->{mod12}[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

Trojan.
 
If you have trouble navigating TrojanWarBlade's code, here's the basics behind what happened here:

stat() returns an array of information about the file. If you need more info about stat, run `perldoc -f stat` in a command prompt or terminal. Index 9 of that array contains the modified time of the file in question, so to get just that info out and ignore the rest you do

Code:
my $mtime = (stat("myfile.txt"))[9];

The modified time is a Unix epoch timestamp, like what the Perl function time() returns - the number of seconds that have passed since 1970.

So if time() is right now, it's bound to be a larger number than $mtime here. Since epoch time is just a number of seconds, it's easy to calculate short distances in time. 60*60 = 1 hour * 12 = 12 hours.

So

Code:
my $mtime = (stat("myfile.txt"))[9];
my $now = time();

my $howLongAgo = $now - $mtime;
print "the file was edited $howLongAgo seconds ago.\n";

if ($howLongAgo <= (60*60*12)) {
   print "the file was edited less than 12 hours ago.\n";
}

Sometimes knowing the bare minimum of how to accomplish a specific task is more useful than having a rather complicated example that involves a lot of different aspects of Perl which, themselves, require some more explanation.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Kirsle,

You forgot to explain this:

Code:
return unless -d $_;


and you forgot to explain the File::Find module usage.

I mean, if you're gonna clean up after me ....

Sorry, could resist teasing. ;-)

Good explanation. :)


Trojan.
 
Question, do I run this from the directory I want to check?
 
Also I noticed that it is recursing into subdiretories. I only want the base info of the directories in a certain directory. For example:

/u/backups has directories for 5 clients that we back up to. I only want the base information about those 5 subfolders, not the subfolders in those 5.
 
In that case it may be more appropriate to just open that directory using opendir and read through the entries manually.

You can prevent File::Find from descending if you want to, which was the subject of another recent thread here...

Annihilannic.
 
Ok all, I am revisiting this idea. What I really need to do is this now.
Given a specific path (i.e. /tmp/backup/client) which is in a variable pulled from a database. Find files modified within the last 1 day so if today is November 5th, find files that were modified on the 4th. If there were any files modified then create a variable $modified = Y. Then count the number of files modified and set a variable $numfiles = x.

If you don't have any idea at what I am getting at just let me know and I will try to elaborate more.
 
Seems clear enough... but what have you tried and where are you stuck?

Annihilannic.
 
I am stuck on how to find modified files and then count how many there are.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top