目的:检查某个文件夹及其子文件夹下的所有文件是否被改动。
思路:
1. 使用stat命令将所有文件的更改时间记录到文件A中。每个被扫描的文件在文件A中被记录为一行。
2. 然后定期检查所有文件的最新更改时间。并将最新的更改时间记录到一个新文件B中。与A一样,每一行会记录一个被扫描文件的最新更改时间。
3. 逐行比较文件A和文件B。若某行不一致,则说明该行所表示的被扫描文件有改动。将这一行打印出来。
#!/bin/bash
# 定时检查文件是否有更改,有更改就打印出更改的文件。
# scan the file tree
scanDir(){
# one parameter
for i in ` ls $1 | sed -e 's/ /\n/g' `;
do
# check if this is a dir.
if [ -d $1/$i ];
then
# iterate to son dir.
checkDir "$1/$i"
else
# print just for debug.
echo "$1/$i"
fi
done
}
# check the second paremter
if [[ $2 =~ ^[0-9]+$ ]]
then
sleep_time=$2
echo "set period as $sleep_time seconds"
else
sleep_time=5
echo "default period is 5 seconds"
fi
# put all the scan results to one list.
for i in `scanDir $1`;
do
index=$[ index+1 ]
files[$index]=$i