To monitor files in directory which are older than 5 mins

-homework:

  1. Every five minutes Monitor incoming directory - send an alert if there is any
    file present in incoming directory older than 5 minutes with outputof pwd and ls -lrt.
  2. Every five minutes monitor Failed and Processing directory - send an alert if there is
    any file present in processed or Failed directory with output of pwd and ls -lrt
    -here iam having issue that even when there is no file present the count coming is 1 and if there is file present it is coming 0
    -code which i tried:
#!/bin/bash
while true
do
   for dir in `cat /home/dirlist.cfg`
   do
      cd $dir
      find ${dir} -maxdepth 1 -mmin +5 -type f | xargs ls -lrt > $HOME/5_minoldfile.txt
      export cnt=`grep -v "^$" $HOME/5_minoldfile.txt| wc -l`
      echo $cnt
      if [ "${cnt}" -gt 0 ];then
         export dirtype=`echo $dir |awk -F "/" '{print $NF}'`
         echo "$dirtype"
         if [ $dirtype == "in" ]; then
            echo "*******  file in $dir is is not picked up for processing for more than 5 minutes please check**********"
            cat $HOME/5_minoldfile.txt
            mail -s "File Monitor" ng@abc.com < $HOME/5_minoldfile.txt
         fi
   fi
   done
   rm $HOME/5_minoldfile.txt
   sleep 300
done

ls -lrt might add a "total" line.
You can grep it out, or redirect the find output to the file (unsorted though).

Other thoughts:
do not export all shell variables!
grep -c directly produces the line count.
In commands have a $var in quotes!
cd "$dir" seems redundant if followed by find "$dir". But if cd "$dir"; then can make sense.
Consider dirtype=$( basename "$dir" ) or dirtype=${dir##*/}