RSS Feed for This PostCurrent Article

Unix: Find Files Older Than X Hours

Here is a simple script to find files older than X hours

#!/bin/ksh
 
# time in seconds. E.g 7200 = 2 hours
TIME_SECONDS=7200
 
# directory to search
DIR=/tmp
 
# touch file
TMP_FILE=/tmp/flagfile
 
STAMP=$(perl -e '($ss, $mm, $hh, $DD, $MM, $YY) = localtime(time() - $TIME_SECONDS);
printf "%04d%02d%02d%02d%02d", $YY + 1900, $MM + 1, $DD, $hh, $mm')
 
touch -t $STAMP $TMP_FILE
 
#----find older files
find  $DIR -type f ! -newer $TMP_FILE -print |\
while read FILE
do
  # list out the file only now. Do what you want here
  ll $FILE
done

Alternatively, you can use the GNU findutils which allows searching by hour


Trackback URL


RSS Feed for This Post2 Comment(s)

  1. Oleg | Mar 24, 2009 | Reply

    man find say:
    Numeric arguments can be specified as
    +n for greater than n,
    -n for less than n,
    n for exactly n.

    -amin n
    File was last accessed n minutes ago.

    -anewer file
    File was last accessed more recently than file was modified…

    -atime n
    File was last accessed n*24 hours ago.

    -cmin n
    File’s status was last changed n minutes ago.

    -cnewer file
    File’s status was last changed more recently than file was modified.

    -ctime n
    File’s status was last changed n*24 hours ago.

    -mmin n
    File’s data was last modified n minutes ago.

    -mtime n
    File’s data was last modified n*24 hours ago.

  2. Oleg | Mar 24, 2009 | Reply

    find . -type f -mmin +120 -print

Sorry, comments for this entry are closed at this time.