In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to write a Shell script that monitors changes in LINUX directories and files". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to write a Shell script that monitors changes in LINUX directories and files.
Script 1: save the original state of the directory to be monitored to the LOG log
Script 2: compare the original state of script 1 with this script, and save the changes to the log if the directory file changes.
Note: the principle actually uses the output value of du-sb to judge the change of the file, and then uses diff to compare it.
1. Save the original state before executing the script:
# vi initial.sh
The code is as follows:
#! / bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin:~/bin
Export PATH
# directories for monitoring
DIR=/root
# temporary files
TMP_A=/tmp/a.txt
# iterate through the file size and path under the specified directory and redirect to the log file
Find $DIR-print0 | xargs-0 du-sb > $TMP_A
two。 Execute monitoring script
# vi monitor.sh
The code is as follows:
#! / bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin:~/bin
Export PATH
# directories for monitoring
DIR=/root
# date variable
DATE= `date +% favored% 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
# iterate through the file size and path under the specified directory and redirect to the log file
Find $DIR-print0 | xargs-0 du-sb > $TMP_B
# compare directory changes and write the changed files to the log
DIFF=$ (diff $TMP_A $TMP_B)
If [[- z $DIFF]]
Then
Echo "Nothing change" > > $LOG
Else
Echo "Here is the change" > > $LOG
Echo "" > > $LOG
Echo "$DIFF" | awk'{print $3}'| 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 to the initial state
Find $DIR-print0 | xargs-0 du-sb > $TMP_A
Fi
Fi
Echo "=" > > $LOG
# cleaning up temporary files
Rm-rf $TMP_B $TMP_C
At this point, I believe you have a deeper understanding of "how to write a Shell script that monitors changes in LINUX directories and files". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.