1832979 Members
3655 Online
110048 Solutions
New Discussion

Script Questions

 
SOLVED
Go to solution
Andre Lemon
Regular Advisor

Script Questions

Is it possible to create a script that will check to see which files have been modified and or created in the past # of hours? I want to check this due to a directories space
jumping quite a bit at various times of the day. I want to try to pinpoint as much as possible what is causing this.
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor
Solution

Re: Script Questions

Hi Andre:

# touch -amt 11200001 /tmp/myref
# find /path -xdev -type f -newer /tmp/myref

The above will create a reference point beginning at November 20 at 0001. The 'find' will look for *files* in the '/path' that have been created or modified since then. The '-xdev' option prevents crossing mountpoints -- most useful if you want to search the root ('/' directory.

See the manpages for 'find' for more information.

Regards!

...JRF...
Andre Lemon
Regular Advisor

Re: Script Questions

James, I am new to scripting. How would I be able to do this check every 2 to 3 hours?
I would somehow have to make that reference point time be 2 or 3 hours prior to the time that I do the check. Is there a way to do that?
Dennis Handly
Acclaimed Contributor

Re: Script Questions

>I would somehow have to make that reference point time be 2 or 3 hours prior to the time that I do the check.

By using date +%m%d%H%M you can get: 11201344

By breaking apart date, you can get just the %H field and subtract 3. And if negative, you can get the %d field and subtract 1 and add 24 to the %H field. You may need to use printf(1) to make sure each field is two digits.

And more checks for beginning of the month and year.
James R. Ferguson
Acclaimed Contributor

Re: Script Questions

Hi (again) Andre:

OK, let's create a tiny script that you can modify and 'cron':

# cat .findit
#!/usr/bin/sh
typeset REF=/tmp/myref
touch ${REF}
perl -e '$t=time()-(60*60*3);utime($t,$t,@ARGV)' ${REF}
find /path -xdev -type f -newer ${REF}
exit 0

...This script creates a "referenece" file named by ${REF}. A Perl snippet is used to set the file's modification and access time to some number of hours ago from the current time. I used 3-hours in the code. The reference file is then used in a 'find'.

If you want to run this automatically every
hours, make a crontab entry like:

0 0,3,6,9,12,15,18,21 * * * /home/andre/findit

This will run your script every thre hours beginning at midnight. See the manpages for 'crontab' for more information.

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: Script Questions

Hi,

use an appropriate TZ variable for a changed timezone for this purpose:
Actual timezone: TZ=MET-1
Three hours before: TZ=XXX2 (-1 + 3)

Create a reference file via
touch -m -t $(TZ=XXX2 date +%m%d%H%M) /tmp/myref

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Andre Lemon
Regular Advisor

Re: Script Questions

Thank you gentleman for the help so far. I have to work on something else prior to leaving for the day. I will try these out in the morning and come back to assign points.


Thanks as always

Andre'
Hein van den Heuvel
Honored Contributor

Re: Script Questions

In these threads someone always points out that HPUX (and unix in general) does not remember the 'create date'. That said, for the question on hand teh last modified time will work fine for that.

If you are comfortabel trying perl, then check out its '-M ' file function:

-M Script start time minus file modification time, in days.

Add to that a 'glob' and you might be in business:

perl -e "while (<*.txt>) { print "$_\n" if 3/24 > -M }"


hth,
Hein.

Yogeeraj_1
Honored Contributor

Re: Script Questions

hi,

you can also use the "at" command inside your script to re-schedule itself after it has executed.

e.g.
echo " /dev/null

see "man at" for more details.


kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Dennis Handly
Acclaimed Contributor

Re: Script Questions

>Yogeeraj: you can also use the "at" command inside your script to re-schedule itself
echo " /dev/null

A better way would to just use the -f option and give a relative time:
at -f /path-to/your-script.ksh +2 hours > /dev/null
Andre Lemon
Regular Advisor

Re: Script Questions

Thank you all for your help. I have learned some new ways of doing things.


Thanks a bunch.


Andre'