Monitor files and directories when it has changed

The code I created monitors directories, files and reports when it has changed. It saves the md5sums of the files it is monitoring and sends alert whenever any files change.

It sends an excel report to admin. My code is not working. I am getting error messages.


log.xlsx - the log output should look like the following:

Permission  userid   group        time           size            file         status              
-rwxr--r--    c66770   dephrs  1:00:00:00   1435    report.java    appeared
-rwxr--r--    c74570   dephrs  2:00:00:00   6993    report2.java   changed
rwxr--r--     c34569   dephrs  2:00:00:00   6993    report3.sas    disappeared


#!/bin/bash


MONITORDIR=/opt/log.xlsx

cd /opt

NOW='date + %d%B%Y%H%M%S '

 "$MONITORDIR" > /opt/log.xlsx

   for fILE in ls -l $(find /foo/* -type f ) 
    do 
      md5sum "$(FILE)" cut -d " " -f1 > "$(MONITORDIR]"/"$(FILE)"
    done

  while :
  do

  NOW='date + %d%B%Y%H%M%S '

   for fILE in ls -l $(find /foo/* -type f ) 
     do 
  
   prev = 'cat "$(FILE)" || echo 0'
   if [-s "$(FILE); then

#it exists and has content
   md5=md5sum "$(FILE)" cut -d " " -f1 > "($MONITORDIR)"/"$(FILE)"
   
   if ["$prev" != "$md5"]; then

   case "$prev" in 
    0) echo "'date' $FILE appeared."
   *) echo "'date' $FILE changed."
   esac
  cp "$[FILE]" "$(MONITORDIR)/$(FILE).$NOW"
  
 fi
  else

# doesn't exist; did it exist before?

   if [$prev !=0]; then
   echo "date' :$FILE disappeared."
   echo > 0 "$(MONITORDIR)/$(FILE)"
   fi
 fi
 done
 sleep 10
 done

 mail -a /path/to/file/foo.xlsx -s "monitor directories and files report"         mary.email@example.com; 
jim.email@example.com

Open in new window

dfn48Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
What is
/opt/log.xlsx > /opt/log.xlsx
intended to do?

There seems to be an unmatched quote in
 if [-s "$(FILE); then

Did you mean
prev = `cat "$(FILE)" || echo 0`

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmcgOwnerCommented:
This code seems like a pretty expensive way to achieve the desired result. When you get a little more advanced, you may want to look at the inotify API (if you're using Linux).

If you're on a system where the test command has been aliased to "[", you may be able to get away with unmatched brackets, but you do have to leave a space after the [ for the shell to invoke the alias.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.