script to check files older than 1 hour

November 16, 2010 at 14:07:27
Specs: Solaris 10

The find command on Solaris makes it hard to find files that are older than 1 hour...I can easily find those that are older than 1 day, but I need more granularity. Other than switching to Linux, which is not an option, any ideas?

From the command line, I can ls -l and awk the time element out, but what can I compare it to? Say the ls -l command shows a file 'myfile' with a last-modified time of '10:30'...Now, how do I get a 'now' time so I can do a comparison for 10:30 < now - 1 hour ?


See More: script to check files older than 1 hour

Report •

November 16, 2010 at 22:44:15

One way is to use the -newer option of the find command. This command creates a file with a time stamp of 2200 on 11/16/10:

touch -t 201011162200 /tmp/myfile

Now, this command finds all the files in the current directory older than the creation time of myfile:

find . -type f ! -newer /tmp/myfile

Report •

November 17, 2010 at 14:58:07

Thank you for your reply! However there's a problem in that I have to dynamically set the time on a reference file, so I went with a simpler solution - still blending sh and perl:

# where outDir is the output directory we want to clean up every hour
for i in `ls -1 $outDir/*.xyz`
rm -f `perl -e 'use File::stat;$sb = stat("'$i'"); if ( (time - ($sb->mtime)) > 3600 ) { print "'$i'" };'`
# difference between the current time and file modified time, if it is > 3600secs..
# print the name of file, which will be removed by the rm command.

Report •

November 17, 2010 at 16:39:56

Just in case anyone is interested, this is how I was going to do it purely with sh,
except that I was going to delete the files if they weren't newer than yesterday. This is just my testfile

test=`date +%m%d%H%M`
yesterday=`expr $test - 10000`
touch -t $yesterday ./tfile
find . -name "*.xyz" ! -newer ./tfile -print

Report •

Related Solutions

Ask Question