0
#!/bin/bash

cd /home/pi/cc/uvreadings

while true
do
   ATIME=`stat -c %Z /home/pi/cc/uvreadings/uvreadings.log`

   if [[ "$ATIME" < "$LTIME" ]]
   then
       echo "log file not updated for +24 hours"
   else
       echo "log file WAS updated in last +24 hours"
   fi

   sleep 10
done

I am trying to check if a file has been modified in the last 24 hours by comparing atime to Ltime

atime will always be less than ltime

so can i modify the statement

if [[ "$ATIME" < "$LTIME" ]]

to

if [[ "$ATIME +1 day" < "$LTIME" ]]`

of is the a better way to achieve this

thanks for any advice

markp-fuso
  • 28,790
  • 4
  • 16
  • 36

1 Answers1

0

First of all, there are (at least) three different timestamps on a file, and you seem to have them mixed up. You talk about atime, the %Z option instead gives you ctime, and it sounds like what you want is actually mtime: the time at which the file's contents last changed. You can get mtime with stat -c %Y.

To see if it was less than one day ago, you can indeed add one day to it; but the time is in seconds, so you need to add 24*60*60 seconds. The easiest way to do that is probably with arithmetic expansion. It could look something like this:

if [[ $((MTIME + (24 * 60 * 60))) > $LTIME ]] # modified in the last day

I assume you wrote code to get the current time and store it in LTIME, even though you didn't show that code in your question. It has to go inside the loop, of course. One way to do that would be

LTIME=`date +%s`
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • Hi thanks for that explanation very helpful and informative, have it running now with the help of all the above comments thanks again – vigilance wx Feb 07 '20 at 16:29