Articles

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 ?

Thanks!


See More: script to check files older than 1 hour

Report •


#1
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 •

#2
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`
do
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.
done



Report •

#3
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

#!/bin/sh
outDir=/tmp
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