How to tell if a file is older than 30 minutes on AIX?

I'd like to write a shell script in ksh or bash which exits with 1 if a specific file is older than 30 minutes. (Last modification time is older than half hour). It would be easy on a Linux or a modern unix, but I have to do it on AIX 5.2 version.

So the constraints:

  • there is no -mmin -mmax options in 'find'
  • there is no -d option in touch (touch -d '30 minutes ago' temp then find ! -newer temp doesn't work)
  • there is no 'stat' command
  • there is no %s parameter in the 'date' command (date +%s doesn't work)
  • I'm not allowed to install any software

Could you please help how can I do it with these constraints?


Solution 1:

I can do it using just touch, test, and date. (tested on AIX 5.3.0.0)

First you need to create a file 30 minutes in the past (Unfortunately, this requires prior knowledge of the current timezone on the machine. But you may be able to work that into things if need be.)

In this example, the current timezone is EST5EDT (GMT-4). If you're lucky, the machine timezone will be set at GMT and you can just use TZ=00:30:

-bash-3.00# date
Mon 26 Mar 14:22:31 2012
-bash-3.00# touch -t `TZ=04:30 date '+%Y%m%d%H%M.%S'` foo
-bash-3.00# ls -al foo
-rw-r--r--   1 root     system            0 26 Mar 13:52 foo

Now use test to compare the dates:

-bash-3.00# test '/smit.log' -ot foo && echo 'smit.log is older than 30min'
smit.log is older than 30min

Solution 2:

Use ls.

ls -l lists the last modification time of the file.
You can dissect the output with awk or cut and then parse the date.

Note that on AIX (as on most *nix systems) the format of the date provided by ls depends on how long ago the file was last modified. See the documentation for specifics.

Solution 3:

If you have the basics, bash/tsh and sed. You can write a simple script which will test the filetime associated with ls output:

#/bin/bash

LSOUTPUT=`ls -l`
IFS='
'

for f in $LSOUTPUT; do
  echo $f

  _sedout=`echo $f|sed 's/.*....-..-.. ..:\(..\).*/\1/'`

  if [ "$_sedout" -gt 30 ]; then
    echo "Passed!"
  fi

  echo
done

You will have to vary your sed regex to pull the right location, but this should work as long as you have sed. If ls doesn't provide enough detail, I believe there is a command called istat you can use.

If ksh is your shell, the syntax should be very similar to that of bash. I just used that example because I'm on Ubuntu.