Watching individual files
Use file checksums here's a sample script that would get you going.
#!/bin/bash
# do not put tabs before paths for second file through last file
files_to_check="/path/to/first/file
/path/to/second/file
/path/to/.../file"
delay=30
while true
do
while read path
do
[ ! -e "$path.sha256" ] && sha256sum "$path" > "$path.sha256"
status=$(sha256sum --quiet -c "$path.sha256" > /dev/null 2>&1 && echo "No change!" || (echo "File changed!" && sha256sum "$path" > "$path.sha256"))
[ "$status" == "No change!" ] && color="\e[0m" || color="\e[38;5;208m"
printf "$color[%s] %s: %s\e[0m\n" $(date +%Y%m%d.%H%M%S) "$path" "$status"
done <<< "$files_to_check"
sleep $delay
echo "-----------"
done
If you just want to check an entire directory recursively, you could pass the output of find /dir/to/check -type f
to the while loop instead of the file list.
Output from the script above looks something like this
[20170413.095401] a: No change!
[20170413.095401] b: No change!
-----------
[20170413.095431] a: No change!
[20170413.095431] b: No change!
-----------
[20170413.095501] a: File changed!
[20170413.095501] b: No change!
Scanning an entire directory and watching for addition/deletion/modification of files
This script will scan an entire directory every 30 seconds (not including the time it takes to run the scan) and color additions (green), removals (red), and changes (orange).
#!/bin/bash
# do not put tabs before paths for second file through last file
dir_to_check="/path/to/directory"
delay=30
# please note that the time it takes to generate checksums will produce more time
# between loops than the actual delay
while true
do
while read path
do
[ ! -e "${path/.sha256/}" ] && printf "\e[38;5;196m%s\e[0m\n" "[$(date +%Y%m%d.%H%M%S)] File removed: ${path/.sha256/}" && rm "$path"
done <<< "$(find $dir_to_check -type f -iname "*\.sha256" | sort)"
while read path
do
if [ ! -e "$path.sha256" ]; then
printf "\e[38;5;046m%s\e[0m\n" "[$(date +%Y%m%d.%H%M%S)] New file found: $path (modified $(date --date="@$(stat --format="%Y" "$path")" +%Y%m%d.%H%M%S))"
sha256sum "$path" > "$path.sha256"
else
status=$(sha256sum --quiet -c "$path.sha256" > /dev/null 2>&1 && echo "No change!" || (echo "File changed! ($(date --date="@$(stat --format="%Y" "$path")" +%Y%m%d.%H%M%S))" && sha256sum "$path" > "$path.sha256"))
[ "$status" == "No change!" ] && color="\e[0m" || color="\e[38;5;208m"
printf "$color[%s] %s: %s\e[0m\n" $(date +%Y%m%d.%H%M%S) "$path" "$status"
fi
done <<< "$(find $dir_to_check -type f ! -iname "*\.sha256" | sort)"
sleep $delay
echo "-----------"
done
Output resembles (sorry, color doesn't transfer here)
[20170414.103126] /tmp/change/a: No change!
[20170414.103126] /tmp/change/b: No change!
[20170414.103126] /tmp/change/c: No change!
[20170414.103126] /tmp/change/run.sh: File changed! (20170414.103125)
-----------
...
-----------
[20170414.103156] File removed: /tmp/change/a
[20170414.103156] /tmp/change/b: No change!
[20170414.103156] /tmp/change/c: No change!
[20170414.103156] /tmp/change/run.sh: No change!
-----------
[20170414.103206] New file found: /tmp/change/a (modified 20170414.103200)
[20170414.103206] /tmp/change/b: No change!
[20170414.103206] /tmp/change/c: No change!
[20170414.103206] /tmp/change/run.sh: No change!
inotifywait
) – steeldriver Apr 13 '17 at 01:26