How to monitor Linux directory file changes using the shell

Source: Internet
Author: User
Tags diff file size linux


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/


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.