Recently saw a group of people chatting to their server has recently been Hung horse, and then want to use a script to monitor the Web directory changes in real-time files, that is, the directory of additions and deletions to the corresponding log records.
At that time feel this function is really a bit interesting, so I looked up some information on the Internet, I studied the next, now have time to write a script, to share with you, if there is a problem, but also please point out.
I've written two scripts below:
Script 1: Save the original state of the directory that needs to be monitored to log logs
Script 2: Compare the original state of script 1 with this script, and save the changed content to the log if the directory file changes.
Note: The principle actually utilizes the DU-SB output value to judge the file change, then uses the diff to carry on the comparison.
1. To save the original state before executing the script:
# VI initial.sh
-------------------
#!/bin/bash
Path=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin:~/bin
Export PATH
# The Directory to monitor
Dir=/root
# Temporary files
Tmp_a=/tmp/a.txt
# traverse the file size and path in the specified directory and redirect to the log file
Find $DIR-print0 | xargs-0 DU-SB > $TMP _a
-------------------
2. Execute the Monitoring script
# VI monitor.sh
-------------------
#!/bin/bash
Path=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin:~/bin
Export PATH
# The Directory to monitor
Dir=/root
# Date Variable
Date= ' Date +%f_%h:%m '
# Temporary files
Tmp_a=/tmp/a.txt
Tmp_b=/tmp/b.txt
Tmp_c=/tmp/c.txt
# log file
Log=/var/log/filemodify.log
# traverse the file size and path in the specified directory and redirect to the log file
Find $DIR-print0 | xargs-0 DU-SB > $TMP _b
# Compare directory changes and write changed files to log
diff=$ (diff $TMP _a $TMP _b)
if [[Z $DIFF]];
Then
echo "Nothing Change" >> $LOG
Else
echo "Here's the change" >> $LOG
echo "" >> $LOG
echo "$DIFF" |awk ' {print $} ' |sort-k2n |uniq |sed '/^$/d ' |tee $TMP _c >> $LOG
If [-S $TMP _c];
Then
echo "" >> $LOG
echo "It modified at $DATE" >> $LOG
# Overwrite the current monitored directory structure with the initial state
Find $DIR-print0 | xargs-0 DU-SB > $TMP _a
Fi
Fi
echo "====================================" >> $LOG
#Clean up temporary files
RM-RF $TMP _b $TMP _c
--------------------
Author: 51cto Blog All the way north
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/