Alert when files change


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Alert when files change
# 1  
Old 09-27-2015
Alert when files change

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 getting error messages.

log.xlsx - the log output should look like the following:
Code:
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

Here is my code:
Code:
#!/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


Last edited by zaxxon; 09-28-2015 at 04:46 AM.. Reason: some more code tags
# 2  
Old 09-28-2015
What about posting these errors using code tags?

Flying over your code, this for a start:

Code:
"$MONITORDIR" > /opt/log.xlsx

This does nothing. It is missing an echo in front of it.

Code:
md5sum "$(FILE)" cut -d .....

There is no pipe after the md5sum command to hand it's output over to stdin of cut

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

This will not be evaluated. Either use backticks or $(date ...).
There is more of those in your code.

Code:
if [-s "$(FILE); ...

There is missing a blank between the square bracket and the following arguments.
There is more of those in your code as well.

Code:
case "$prev" in 
    0) echo "'date' $FILE appeared."
...

Blocks inside case/esac have to be terminated by ;;.

And there are more and more errors.

I wonder how you can write a script as it seems you have not even tested a single line of it before putting it together.
# 3  
Old 09-28-2015
Usually error messages are quite self explanatory. Why not work them off step by step?
# 4  
Old 09-28-2015
Other then mentioned in previous post there is a quicker way.

Check out -c option of md5sum.

So basically you start a find with absolute paths and run md5sum on every file required redirecting output into a file.

Then you just run
Code:
md5sum -c <file created with absolute paths>

It will report if checksum has changed since the last find ran, which you can parse further.

Other then that, if you are on Linux systems you might as well use inotifywatch so achieve the same (constant logging of changes made and on which files).
Remember, you cannot use it on NFS filesystems.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Cybersecurity

**ALERT**ALERT* Whats app requesting users to upgrade to latest version as; its vulnerability found.

Hello All, Want to share here an article for Whats app users that team whats app is requesting users to upgrade to its latest version since they have found a vulnerability; where Hacker could turn on a cell's camera, mic and scan messages and emails. Here is an article from "THE VERGE" for... (1 Reply)
Discussion started by: RavinderSingh13
1 Replies

2. Shell Programming and Scripting

CLI script for emailing alert if files missing in dir

thread removed (4 Replies)
Discussion started by: billabongjimmy
4 Replies

3. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (7 Replies)
Discussion started by: ust3
7 Replies

4. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (0 Replies)
Discussion started by: ust3
0 Replies

5. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies

6. Shell Programming and Scripting

This script cut size of files "Lol" change string in files

Basic: find . -type f -name “*.txt” -print | awk '{gsub("Ontem", "AntesdeOntem", $0); print > FILENAME}' *.txt The idea is in folder /home/myapontamentos I have some files and i need to change in all them the word "ontem" to "antesdeontem". But bigger files are cut (size i mean)... (4 Replies)
Discussion started by: single
4 Replies

7. Shell Programming and Scripting

script to monitor files in a directory and sending the alert

Hi All, We are having important config files in an directory which was accessable by all /auto/config/Testbed/>ls config1.intial config2.intial config3.inital often we find that some of the lines are missing in config files, we doubt if some one is removing. I would like to write... (0 Replies)
Discussion started by: shellscripter
0 Replies

8. Shell Programming and Scripting

how to change mod of files

hi everyone I have a directory /usr5/sch. The shell is /bin/csh/. In this directory, I have a script called cs-claims-tst that runs an object cobol that going over a few other directory and open files and inserting them to d.b! If i run this script from its current directory, it works o.k, ... (8 Replies)
Discussion started by: naamas03
8 Replies

9. UNIX for Dummies Questions & Answers

ALERT!!! ALERT!!! I messed up the UNIX!!!

Hello If you want a short description read my last thread! I have no root access anymore. No remote access! By user access i get a programm started and when i close it, i got logged off! So I guess I messed up the passwd file! is there a chance of booting a unix system (SCO-UNIX UnixWare... (15 Replies)
Discussion started by: Fwurm
15 Replies
Login or Register to Ask a Question