Обратите внимание, что следующий фрагмент кода НЕ работает с именами файлов, содержащими пробелы, символы табуляции или символы новой строки.
Чтобы узнать больше об используемой здесь команде find
-, см.https://unix.stackexchange.com/a/240424/364705
#!/bin/bash
lastrun=''
while true; do
read -r newer older <<< \
$(find. -type f -exec stat -c '%Y %n' {} \; \
| sort -nr \
| awk 'NR==1,NR==2 {print $2}'\
| xargs )
if [ "$newer" == "$lastrun" ]; then
:
else
if ! cmp "$newer" "$older" > /dev/null ; then
base64 "$newer"| tr -d '\n' | sed '$ a \'
lastrun="$newer"
fi
fi
done
И решение с использованиемinotify-wait
:
#!/bin/bash
lastfile=''
inotifywait -m /path/to/somewhere -e close -e moved_to |
while read path action file; do
if [ "$lastfile" != '' ];then
if ! cmp "${path}$file" "${path}${lastfile}" > /dev/null ; then
base64 "${path}${file}"
fi
fi
lastfile="$file"
done