1825001 Members
2601 Online
109678 Solutions
New Discussion юеВ

File time check

 
Luis E. Giner_1
Occasional Contributor

File time check

I need to shell command/script that can check the files in a directory to see if they're older than XX minutes. The find command only allows for days. Any suggestions?
7 REPLIES 7
Rodney Hills
Honored Contributor

Re: File time check

Use the touch command to set the date/time onto a file attribute, then use "newer" option in find-

touch 200405240800 /tmp/afile
find . ! -newer /tmp/afile -print

HTH

-- Rod Hills
There be dragons...
Luis E. Giner_1
Occasional Contributor

Re: File time check

That's a great idea but this will be automated. How do I tell the script to touch a file but make it 60 minutes older than the current time? Thanks for your time and suggestions.
Rodney Hills
Honored Contributor

Re: File time check

Correction on "touch" command

touch -t 200405230815.30 /tmp/afile

Will set /tmp/afile last modify time to 5/23/2004 8:15:30

-- Rod Hills
There be dragons...
Fred Ruffet
Honored Contributor

Re: File time check

CurrentDate=`date +%Y%m%d%H%M`
Date2Match=`expr $CurrentDate - 60`
touch -t $Date2Match myfile.$$
find ... -newer myfile.$$ ...
rm myfile.$$

... or something like that :)

Fred
--

"Reality is just a point of view." (P. K. D.)
Rodney Hills
Honored Contributor

Re: File time check

If you don't mind using "perl"-

perl -e 'use File::Find; find(\&wanted,"."); sub wanted { print $File::Find::name,"\n" if (stat($_))[9] < time-3600; }'

This will print files under the current directory that are more than 60 minutes old.

HTH

-- Rod Hills
There be dragons...
Luis E. Giner_1
Occasional Contributor

Re: File time check

The expr part doesn't subtract 60. I get a strange number and the touch gets a bad conversion error.

CurrentDate=`date +%Y%m%d%H%M`
Date2Match=`expr $CurrentDate - 60`
touch -t $Date2Match myfile.$$
find ... -newer myfile.$$ ...
rm myfile.$$
curt larson_1
Honored Contributor

Re: File time check

well for just an hour difference,
you could change your timezone.

such as this, if your in the us eastern timezone.

oldTZ="$TZ"
TZ="CST6CDT"
TZ="$oldTZ"

now=$(date +format)

touch -t "$now" file
etc...

for a more generic solution, you'll probably want to write a perl script to be able to subract an abitrary amount.

or you could use Mr. Stephenson's calendar script from this site at the bottom of the page

http://mirrors.develooper.com/hpux/