Bash to monitor Modified Files/Directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to monitor Modified Files/Directories
# 1  
Old 07-06-2011
Bash to monitor Modified Files/Directories

Hi All ,

I have a directory called "/usr/local/apache/docs/" inside this docs i have below directories ,
Code:
bash-2.05# pwd
/usr/local/apache/docs/
bash-2.05#ls -l | less
   2 drw-r-xr-x   3 root     root         512 Aug  8  2010 Form1
   2 drw-r-xr-x   3 root     other        512 Mar  8  2011 Narrowchart
   2 drw-r-xr-x   3 root     other        512 Mar 23  2011 Followers

Inside above directories[Form1,Narrowchart..etc. ] i have subdirectories and files , I am looking for a bash script to monitor the directory "/usr/local/apache/docs/ continuously , and if any directory or files modified under this directory [/usr/local/apache/docs/ ]or its sundirectories [/usr/local/apache/docs/Form1/] that needs send a email to administrator .

Last edited by pludi; 07-06-2011 at 05:23 AM..
# 2  
Old 07-06-2011
What does "modifed" mean in this context. Modified in the past 30 minutes,1hr or past 2 or 3 days..?
Code:
find /usr/local/apache/docs/ -mmin -30 # finds all files modified in the past 30 minutes
find /usr/local/apache/docs/ -mtime -1 # find files modified within the past 24 hours

# 3  
Old 07-06-2011
read about inotify or rsync
# 4  
Old 07-06-2011
use inotify to monitor file system event
# 5  
Old 07-08-2011
Quote:
Originally Posted by gnanasekar_beem
Hi All ,

I have a directory called "/usr/local/apache/docs/" inside this docs i have below directories ,
Code:
bash-2.05# pwd
/usr/local/apache/docs/
bash-2.05#ls -l | less
   2 drw-r-xr-x   3 root     root         512 Aug  8  2010 Form1
   2 drw-r-xr-x   3 root     other        512 Mar  8  2011 Narrowchart
   2 drw-r-xr-x   3 root     other        512 Mar 23  2011 Followers

Inside above directories[Form1,Narrowchart..etc. ] i have subdirectories and files ,
I am looking for a bash script to monitor the directory "/usr/local/apache/docs/ continuously ,
and if any directory or files modified under this directory [/usr/local/apache/docs/ ]
or its sundirectories [/usr/local/apache/docs/Form1/] that needs send a email to administrator .
I try to write a script for this issue Smilie
Code:
#mkdir newfolder newfolder111 ; touch newfile newfile111 testfile ; echo "1212">>newfileaddbyte ; rm -f file

for example ; we want to check folder activitys for 1 minute interval (60 sec)
Code:
# ./chkmodifs.sh /usr/local/apache/docs 60
Please wait.. Preparing File Lists!!
Lists OK!!
#########################################################
         [Detected change of size]
             -----------------
        File Name(s)            Difference (Bytes)
-----------------------------   ------------------------
/usr/local/apache/docs/newfileaddbyte         +5 bytes
          [End of summary list]
#########################################################
 
#########################################################
      [Detected new created files]                                                       [Type]
         ---------------------                                                      ----------------
> 0 /usr/local/apache/docs/newfile                                                        file
> 0 /usr/local/apache/docs/newfile111                                                     file
> 4096 /usr/local/apache/docs/newfolder                                                directory
> 4096 /usr/local/apache/docs/newfolder111                                             directory
> 0 /usr/local/apache/docs/testfile                                                       file
          [End of summary list]
#########################################################
 
#########################################################
          [Detected removed files]                                                       [Type]
             -----------------                                                      ----------------
< 0 /usr/local/apache/docs/file                                                           file
          [End of summary list]
#########################################################

Code:
# cat chkmodifs.sh
#!/bin/bash
## justdoit ## Simple File Monitor Tool @unix.com ##
if [ ! $# -eq 2 ] ; then echo "Usage '$0 folder waitseconds' " ; exit 1; fi
if [ ! -d $1 ] ; then echo "Folder name is false!!" ; exit 1 ;fi
cd /usr/local/bin ; clear ; tmp='tmppx' ; tmpp="${tmp}x"
f="find '$1' -not \( -name $tmp -o -name $tmpp \) -exec stat -c '%s %n' {} \;"
eval $f >$tmp ; echo -e "Please wait.. Preparing File Lists!!" ; sleep $2 || exit 1 && echo "Lists OK!!"
eval $f >$tmpp ; lcltmp=lcltmp;lcltmpp="${lcltmp}x";lcltmppp="${lcltmpp}x"
cretdif() {
diff $tmp $tmpp|sed -n '/^[><]/p'|sort -k3,3>$lcltmp
}
chksz() {
dtecs=$(awk '{print $3}' $lcltmp|uniq -d)
}
rmvdupf() {
for dup in $dtecs ; do sed 's|.*'$dup'$||;/^$/d' $lcltmp>$lcltmpp && mv $lcltmpp $lcltmp -f;done
}
fnddupf() {
>$lcltmppp
for dup in $(echo "$dtecs"|sed 's|/|\\&|g') ; do
sed -n '/.*'$dup'$/p' $lcltmp>>$lcltmppp;done
}
chkfls() {
newrem=$(sed -n '/^['$1']/p' $lcltmp)
}
fix() {
sed -n 'N;s/[^ ]* \([^ ]*\) \([^ ]*\)\n\([^ ]*\) \([^ ]*\) \2$/\2 \1 \4 \3/p' $lcltmppp|
awk 'BEGIN {printf "%20s" "%30s\n" "%15s" "%10s\n" ,"File Name(s)","Difference (Bytes)","-----------------------------","   ------------------------"};
{if($4==">")printf "%20s" "%10c" "%d" "%2s", $1 ,"+",$3-$2, " bytes";
else printf "%20s" "%10d" "%2s\n", $1, $2-$3, " bytes"};
END { printf "\n%31s\n%19s\n\n" ,"[End of summary list]","#########################################################" }'
}
cretdif;if [ ! -s $lcltmp ] ; then echo -e "No any changes!!..";exit 0;fi
chksz;if [ ! "$dtecs" = "" ] ; then printf "%s\n%34s\n%30s\n" "#########################################################" "[Detected change of size]" "-----------------";fnddupf;fix;fi;rmvdupf
chkfls '>';if [ ! "$newrem" = "" ] ; then 
printf "\n%s\n%34s%+61s\n%+30s%70s\n" "#########################################################" "[Detected new created files]" "[Type]" "---------------------" "----------------"
echo "$newrem"|awk '{if (!system("test -d " $3) )printf "%-60s" "%+36s\n",$0,"directory"; else printf "%-60s" "%+34s\n" ,$0,"file"};
END { printf "\n%31s\n%19s\n\n" ," [End of summary list]","#########################################################" }';fi
chkfls '<';if [ ! "$newrem" = "" ] ; then 
printf "\n%s\n%34s%+61s\n%+30s%70s\n" "#########################################################" "[Detected removed files]" "[Type]" "-----------------" "----------------"
echo "$newrem"|awk '{if (!system("test -d " $3) )printf "%-60s" "%+36s\n",$0,"directory"; else printf "%-60s" "%+34s\n" ,$0,"file"};
END { printf "\n%31s\n%19s\n\n" ,"[End of summary list]","#########################################################"}';fi

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need BASH Script Help to Move Files While Creating Directories

I've got this script to loop through all folders and move files that are more than 2 years old. I'm using the install command because it creates the necessary directories on the destination path and then I remove the source. I'd like to change the script to use the mv command since it is much... (4 Replies)
Discussion started by: consultant
4 Replies

2. Shell Programming and Scripting

Autojump with modified bash prompt

I use the .bashrc file from this thread. Direct link to the archive containing the ultimate bashrc I am trying to use autojump, but it will not autoload the directories to the autojump list as the custom prompt is not compatible. Here is the thread on the issue from other users. apt -... (2 Replies)
Discussion started by: genehunter
2 Replies

3. Shell Programming and Scripting

Help with finding the latest modified version of a file within directories

I am trying to look into multiple directories and pluck out the latest version of a specific file, regardless of where it sits within the directory structure. Ex: The file is a .xls file and could have a depth within the directory of anywhere from 1-5 Working directory - Folder1... (6 Replies)
Discussion started by: co21ss
6 Replies

4. UNIX for Dummies Questions & Answers

Moving Directories Based on Modified date

Hi, How can I move directories (and all sub directories/files) from one directory to another based on the modified date of the directory? Currently the existing structure looks like this: /public_html/media/videos/tmb/34947/image1.jpg /public_html/media/videos/tmb/34947/image2.jpg ... (0 Replies)
Discussion started by: lbargers
0 Replies

5. Shell Programming and Scripting

Comparing the modified dates of files in two directories

Hi Is it possible to compare the modified dates of all the files in two directories using shell script? I would like to take a backup of a directory in production server regularly. Instead of copying all the files in the directory, is it possible to list only the files that are... (2 Replies)
Discussion started by: ashok.k
2 Replies

6. Shell Programming and Scripting

Bash scripting to compare N number of files located in two directories

I want to compare "N" (around 2000+) number of huge files located in a directory A against "N" files located in a different directory using Bash scripting. Please help me with any scripts available. Thanks. (2 Replies)
Discussion started by: Sangtha
2 Replies

7. UNIX for Dummies Questions & Answers

Find most recently modified directories

How do I do it? Simple answers preferred... using BASH.. the less code the better. I want to find out where Indesign is caching PDF tmp data ... I figure this is a good way to do it.. either way i wanna know how to do it. (2 Replies)
Discussion started by: glev2005
2 Replies

8. Shell Programming and Scripting

Identifying New and Modified Files/Directories

Hi. Our shop is migrating to a new UNIX server and our hope is to do a full migration of all files to the new server weeks in advance of the final migration. As a result we want to identify files on our SOLARIS 8 UNIX server that have changed or that were created after a specific date & time... (2 Replies)
Discussion started by: buechler66
2 Replies

9. Shell Programming and Scripting

Bash and Awk for creating directories and moving files

I have a security system that FTPs the camera files to my machine, however I want to sort the pictures (taken every 30s) into directories by hour. Every picture uses the following file format. yymmddhhmmsstt.jpg (where tt is the milliseconds) I am thinking the for loop is best for file... (11 Replies)
Discussion started by: Kiint
11 Replies

10. Shell Programming and Scripting

Last modified file in 2 or more directories

Hi, Is there any simple way to get the last modified file in a set of 2 or more directories? This should return one file only (not 1 file per directory) Thanks for your help (4 Replies)
Discussion started by: m69w
4 Replies
Login or Register to Ask a Question