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    appeared
-rwxr--r--    c74570   dephrs  2:00:00:00   6993   changed
rwxr--r--     c34569   dephrs  2:00:00:00   6993    disappeared



cd /opt

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

 "$MONITORDIR" > /opt/log.xlsx

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

  while :

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

   for fILE in ls -l $(find /foo/* -type f ) 
   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."
  cp "$[FILE]" "$(MONITORDIR)/$(FILE).$NOW"

# doesn't exist; did it exist before?

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

 mail -a /path/to/file/foo.xlsx -s "monitor directories and files report";

Open in new window

Who is Participating?

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

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.

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
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.