I need to check the last modified date of all files within a directory to see if the latest file has been modified within some arbitrary period. Eg. 200 seconds.
Here's the twist, it also has to be a one-liner (its a Marathon health check and I can't rely on a file system being there for a script file).
Here's what I have so far:
ls -v | tail -n 1 | expr $(date +%s) - $(xargs date +%s -r) | if [ $PREV -gt 100 ]; then echo 1; else echo 0; fi
This code works.
if [ `expr $(date +%s) - $(stat -c %Y $(ls -t | head -n 1))` -gt 100 ];then echo 1;else echo 0;fi
How does it work?
ls -t - lists the files based on the time.
head -n 1 - gives first latest file.
date +%s gives time elapsed since epoch.
stat -c %Y - gives modification time since epoch
expr - subtracting present date from modified file date.