Bash script for new file in ftp folder

Hello,
I'm trying to make a bash script that send me e-mail if there is any new file in my ftp folder.

cat inotify.sh
#!/bin/sh

/usr/bin/inotifywait -e create \
    -mrq /home/mrowcp | while read line; do
    echo -n "$line " >> /var/log/inotify.log
    echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done

So at the /var/log/inotify.log I've got:

/home/mrowcp/ CREATE filefile Wed Mar 7
/home/mrowcp/ CREATE filefile123 Wed Mar 7
/home/mrowcp/ CREATE newfilecreate.txt Wed Mar 7

Now the problem is in mail sending.
How Can I send each of this new created files?May be I need another script for inotify.log monitoring, and if new line is created:

tail -n 1 /var/log/inotify.log |cut -d " " -f 3

I would suggest a cron job that run every N mins and checks for any new entries in the inotify.log file. I will need to "remember" where in the log it has already processed and I'd suggest a dat file like /home/mrowcp/.inotify.dat.

The script can then find any new entries from after this line:

CHKFILE=/home/mrowcp/.inotify.dat
LOGFILE=/var/log/inotify.log
NOTIFY=mrowcp@domain.com
 
show_details()
{
    echo " New file(s) have arrived: "
    echo "$newfiles" | sed 's/^/    /'
}
 
# Last line to looked at - just in case a file arrives while we are working
EL=$(wc -l $LOGFILE | awk ' { print $1 }')
 
if [ -f $CHKFILE ]
then
    SL="$(cat $CHKFILE)"
    # Old position is greater than size of file - It's may have been truncated so start from begining
    [ $EL -lt $SL ] && SL=0
else
  # No remembered previous line - start from begining
  SL=0
fi
 
newfiles=$(awk -vS=$SL -vE=$EL '
   NR>E{exit}
   /^\/home\/mrowcp\/ CREATE/&&NR>=S{print $3}' $LOGFILE)
if [ -n "$newfiles" ]
then
   show_details | mail -s "new FTP files" $NOTIFY
fi
echo "$EL" > $CHKFILE

Thanks bro.There is some bugs like: If I upload/receive 3 or more files:

/home/mrowcp/ CREATE aaaaaaaa Thu Mar 8
/home/mrowcp/ CREATE bbbbbbbb Thu Mar 8
/home/mrowcp/ CREATE cccccccc Thu Mar 8

1st time they all are reported to me:

 New file(s) have arrived: 
      fffffffff
      aaaaaaaa
      bbbbbbbb
      cccccccc

, but when the script start again after 1 min. I receive the last one (file) like new one:

New file(s) have arrived: 
      cccccccc

And every time when script start, I receive last uploaded file, like a new.

bash -x checker.sh
+ CHKFILE=/home/mrowcp/.inotify.dat
+ LOGFILE=/var/log/inotify.log
+ NOTIFY=it@freshlogic.bg
++ awk ' { print $1 }'
++ wc -l /var/log/inotify.log
+ EL=31
+ '[' -f /home/mrowcp/.inotify.dat ']'
++ cat /home/mrowcp/.inotify.dat
+ SL=31
+ '[' 31 -lt 31 ']'
++ awk -vS=31 -vE=31 '
   NR>E{exit}
   /^\/home\/mrowcp\/ CREATE/&&NR>=S{print $3}' /var/log/inotify.log
+ newfiles=cccccccc
+ '[' -n cccccccc ']'
+ mail -s 'new FTP files' mail@test.mail.com
+ show_details
+ echo ' New file(s) have arrived: '
+ sed 's/^/    /'
+ echo cccccccc
+ echo 31

P.S. Can you give me example with mutt instead of mail.If I must attach every received file in my mail.

Looks like the .inotify.dat file is getting into the log file so probably best to just filter that one out.

You could also put the .inotify.dat file in another directory as the awk code only looks for files in the /home/mrowcp/ directory

CHKFILE=/home/mrowcp/.inotify.dat
LOGFILE=/var/log/inotify.log
NOTIFY=mrowcp@domain.com
 
show_details()
{
    echo " New file(s) have arrived: "
    echo "$newfiles" | sed 's/^/    /'
}
 
# Last line to looked at - just in case a file arrives while we are working
EL=$(wc -l $LOGFILE | awk ' { print $1 }')
 
if [ -f $CHKFILE ]
then
    SL="$(cat $CHKFILE)"
    # Old position is greater than size of file - It's may have been truncated so start from begining
    [ $EL -lt $SL ] && SL=0
else
  # No remembered previous line - start from begining
  SL=0
fi
 
newfiles=$(awk -vS=$SL -vE=$EL '
   NR>E{exit}
   /^\/home\/mrowcp\/ CREATE/&&NR>=S&&$3!=".inotify.dat"{print $3}' $LOGFILE)
if [ -n "$newfiles" ]
then
   show_details | mail -s "new FTP files" $NOTIFY
fi
echo "$EL" > $CHKFILE