Send email if latest file in a directory is older than 2 hours

I have a objective of Sending email if latest file in a directory(excluding files of sub-dirs) is older than 2 hours.

eg :

 ls -ltr
 drwx--x--x 2 abcde abc 256 2017-02-07 20:10 Mail
-rw-rw-r-- 1 abcde abc 1170 2017-02-24 17:30 test
-rw-rw-r-- 1 abcde abc 356 2017-03-09 18:00 xyz.csv
-rw-rw-r-- 1 abcde abc 501 2017-03-09 18:05 abc2.csv
-rw-rw-r-- 1 abcde abc 415 2017-03-09 18:06 abc.csv
-rw-rw-r-- 1 abcde abc 34150 2017-03-09 18:39 test.csv
-rw-rw-r-- 1 abcde abc 119 2017-04-27 20:52 hotfix.ini

hotfix.ini is latest file in the directory, so I wish to compare the timestamp of this file with the current time of system and if file is older than 2+ hours then send mail with the time stamp of file:hotfix.ini

It will be mostly a shell script which will run in background every 5 mins and send mail only if latest file(based on timestamp) is older than 2 hours. else would go back to sleep again for 5 mins.

Please tell us what UNIX you are using and what shell. In this case it makes a big difference. Linux has better date/time tools than Solaris for example. So the answer to your question will be different for different OS/shell.

For just the time check, try

[ "$(find . -cmin -120)" ] || echo mail -s "subject one" recipient@url.com

For the youngest file's time stamp, your attempt should work.

Using AIX and KSH

---------- Post updated at 02:24 PM ---------- Previous update was at 08:12 AM ----------

Hi Rudi,

Can you help me with the complete script, I didn't get your solution.