修改
我改进了用户konsolebox的“FolderWatcher脚本”(来自下面的答案:https://stackoverflow.com/a/18597233/2095175) 我添加了这些行来自动移动我系统上正确文件夹中的各种文件(例如/ docs文件夹中的pdf,/图片中的图像和/视频中的视频文件)
extension=${ADDED##*.}
if [ "$extension" = "xz" ] || [ "$extension" = "zip" ] || [ "$extension" = "gz" ] || [ "$extension" = "bz2" ] || [ "$extension" = "7z" ]; then
open $ADDED
fi
if [ "$extension" = "pdf" ] || [ "$extension" = "txt" ] || [ "$extension" = "odt" ] || [ "$extension" = "doc" ] ; then
mv "$ADDED" /boot/home/Docs
alert --idea " $ADDED moved to /boot/home/Docs"
open /boot/home/Docs
fi
if [ "$extension" = "jpg" ] || [ "$extension" = "png" ] || [ "$extension" = "gif" ]; then
mv "$ADDED" /boot/home/Media/Images
alert --idea " $ADDED moved to /boot/home/Media/Images"
open /boot/home/Media/Images
fi
if [ "$extension" = "flv" ] || [ "$extension" = "avi" ] || [ "$extension" = "mp4" ] || [ "$extension" = "mpg" ]; then
mv "$ADDED" /boot/home/Media/Video
alert --idea " $ADDED moved to /boot/home/Media/Video"
open /boot/home/Media/Video
fi
原始问题:
我有以下脚本,它不断检查文件夹的内容,并在插入新文件和/或何时删除时提醒我。按预期完美运作。但是不检查主文件夹中的子文件夹(folder = $(cat /boot/home/FolderWatcher_pref.txt)) 例如,如果我从主文件夹插入/删除文件,我将收到提醒,但如果我在“$ folder”的子文件夹中插入/删除文件,脚本将无法提醒我。我可以在此脚本中更改或添加哪些内容以实现此需求?
我在Haiku OS上,所以像“alert”这样的命令是特定于Haiku的。
#!/bin/bash cat /dev/null > $difffile1 cat /dev/null > $difffile2 #The path where to look is set in a text file, which i can change with a file panel to select any folder folder=$(cat /boot/home/FolderWatcher_pref.txt) tstamp=$(stat --print "%Y" "$folder") while true; do prev=$(ls "$folder" | tr '\n' '\n' > /tmp/prev.txt) sleep 5 if [[ "$folder" == "$folder" && $tstamp -lt $(stat --print "%Y" "$folder") ]]; then after=$(ls "$folder" | tr '\n' '\n' > /tmp/after.txt) difference1=$(comm -2 -3 "/tmp/after.txt" "/tmp/prev.txt">/tmp/Diff.txt) added=$(cat /boot/common/cache/tmp/Diff.txt) difference2=$(comm -2 -3 "/tmp/prev.txt" "/tmp/after.txt">/tmp/Diff2.txt) lost=$(cat /boot/common/cache/tmp/Diff2.txt) difffile1=/tmp/Diff.txt difffile2=/tmp/Diff2.txt FILESIZE2=$(stat -c%s "$difffile2") if [ "$FILESIZE2" == 0 ] then lost=nothing fi FILESIZE1=$(stat -c%s "$difffile1") if [ "$FILESIZE1" == 0 ] then added=nothing fi lost2=$(cat /boot/common/cache/tmp/Diff2.txt) alert --idea "$folder: $added *INSERTED*. $lost *REMOVED*."; echo "$lost2" >>$folder/Removed.txt tstamp=$(stat --print "%Y" "$folder") cat /dev/null > $difffile1 cat /dev/null > $difffile2 else sleep 3; fi done
答案 0 :(得分:1)
为什么不能使用inotifywait或inotifywatch?您可以使用-r
标志使其递归。
看看inotify-tools
您可能也对incron感兴趣。
答案 1 :(得分:1)
我最终进行了自己的修改,但这个有效,不再使用临时文件。它使用find
和process substitution
。
#!/bin/bash
[ -n "$BASH_VERSION" ] && [[ BASH_VERSINFO -ge 3 ]] || {
echo "You need bash version 3.0 or newer to run this script." >&2
exit 1
}
shopt -s extglob
FOLDER=$(</boot/home/FolderWatcher_pref.txt)
REMOVED_LOG="$FOLDER/Removed.txt" ## It's better to place this somewhere not in "$FOLDER" to not confuse timestamps everytime it is updated.
if [[ ! -d $FOLDER ]]; then
echo "Directory does not exist: $FOLDER" >&2
elif read TIMESTAMP < <(exec find "$FOLDER" -type d -printf "%T@\n" | cut -f 1 -d . | sort -nr); [[ $TIMESTAMP != +([[:digit:]]) ]]; then
echo "Unable to get timestamp of directory: $FOLDER" >&2
else
STATE=$(exec find "$FOLDER" -mindepth 1 | sort)
for (( ;; )); do
sleep 5
if [[ -d $FOLDER ]] && read NEWTIMESTAMP < <(exec find "$FOLDER" -type d -printf "%T@\n" | cut -f 1 -d . | sort -nr) && [[ NEWTIMESTAMP -gt TIMESTAMP ]]; then
NEWSTATE=$(exec find "$FOLDER" -mindepth 1 | sort)
ADDED=$(comm -2 -3 <(echo "$NEWSTATE") <(echo "$STATE"))
[[ -z $ADDED ]] && ADDED='nothing'
LOST=$(comm -2 -3 <(echo "$STATE") <(echo "$NEWSTATE"))
LOST_ORIG=$LOST
[[ -z $LOST ]] && LOST='nothing'
alert --idea "$FOLDER:
$ADDED
*INSERTED*.
$LOST
*REMOVED*."
[[ -n $LOST_ORIG ]] && echo "$LOST_ORIG" >> "$REMOVED_LOG"
read TIMESTAMP < <(exec find "$FOLDER" -type d -printf "%T@\n" | cut -f 1 -d . | sort -nr) && [[ TIMESTAMP -ge NEWTIMESTAMP ]] || TIMESTAMP=$NEWTIMESTAMP
STATE=$NEWSTATE
fi
done
fi
答案 2 :(得分:0)
你想要扩充这一行:
prev=$(ls "$folder" | tr '\n' '\n' > /tmp/prev.txt)
类似于:
prev=$(ls "$folder"/* | tr '\n' '\n' > /tmp/prev.txt)