Comparing the timestamp of the file to current time

I have a file like this

-rwxr-xr-x 1 rewq other 168 Jan 13 07:05 check_files.sh

I want to compare (check_files.sh time) with the current time to see if its is older than 2 hours or not

if it is not older than 2 hrs then do something.can someone help me on this?.I dont want to use mmin ,im on hp unix which doesnt support mmin option .Thank you

Hello Haadiya,

Following may help you in same.

VAL=`find -type f -mmin +120 -iname "Input_file"`
if [[ -n $VAL ]]
then
       echo "File is OLDER than 2 hours."
else
       echo "File is NOT older than 2 hours."
fi

I have just provided and example for same in if else statements you can provide actions too which you want to perform, hope this helps.

Thanks,
R. Singh

1 Like

Hello Ravi,

Im on hp unix which doesn't support mmin option ,and my filename is dynamic i dont know the file name.what im trying to do is

1) Run the script every 2 hours and check if the last file generated was more than 2 hours old.
below are my tries

 Last file = ` ls -ltr | awk '{print $9}' | tail -1`
 Time of Last file = ` ls -ltr | awk '{print $6,$7,$8}'|tail -1`

Compare with current time to see if its older than 2 hours or not

2) If there is a latest file generated which is not older than 2 hours, check if the latest file is empty.
empty means -
If 'cat filename | wc -l" = 0 (zero lines in the file)

if empty do something

Im working on building this

Hello Haadiya,

Could you please try following and let me know if this helps.

NAME_FILE=`ls -lhtr | awk '($1 !~ /^d/) {print $9}' | tail -1`
CHECK_FILE=`find -maxdepth 1 -type f -name $NAME_FILE -mmin +120`
if [[ -z $CHECK_FILE ]]
then
 if [[ -s $NAME_FILE ]]
 then
  echo "File is NOT empty."
 else
  echo "Do operations as you wish."
 fi
else
 echo "File is OLDER than 2 hours."
fi

Thanks,
R. Singh

1 Like

Do you have a touch command that allows you to create a file 2 hours old? Then use this for the -newer find primary.

1 Like

Hello RudiC,

Yeah you are right,im saw few threads using touch and -newer option,the thing is, im not suppose to create any file in that dir,thats the reason i didnt go for that.

Hello Ravi,
Just an example of mmin in my unix box.I have hp ux 11.3
$ find . -mmin -60 -size 0
find: bad option -mmin
your solution would throw an error ,anyways i ll keep trying

I used something like this earlier

dir="/home/user"
found=`find "$dir" -type f \( -mtime +7200 -o -size 0 \)`
if [ -n "$found" ]; then
 echo "There are files older than 2 hours OR with size 0:
$found"
fi

then realized mmin doesnt work for me.Thanks iI llwork on finding some alternative

You don't need to create that file in the target dir, create it in your home dir. Or in /tmp.

1 Like

If you run the script every 2 hours then simply leave a file behind and compare timestamps in the next run.

me=${0##*/}
lr=/tmp/$me.lastrun
if test -f $lr
then
  for f in $(find . \! -name . -prune -type f -size 0 -newer $lr)
  do
    echo "$f is a new empty file"
  done
fi
>$lr
1 Like