1

编辑:

我已经改进了用户 konsolebox 的这个“FolderWatcher 脚本”(来自下面的答案:https ://stackoverflow.com/a/18597233/2095175 )我已经添加了这些行来自动移动我的正确文件夹中的每种文件系统(例如 /Docs 文件夹中的 pdf、/Picture 中的图像和 /Videos 中的视频文件)

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                                                                                                                                                                                         

    猫 /dev/null > $difffile1
    猫 /dev/null > $difffile2

    #查看路径在文本文件中设置,我可以通过文件面板更改以选择任何文件夹
    文件夹=$(cat /boot/home/FolderWatcher_pref.txt)
    tstamp=$(stat --print "%Y" "$folder")

    虽然是真的;做
    prev=$(ls "$folder" | tr '\n' '\n' > /tmp/prev.txt)
    睡觉 5

    if [[ "$folder" == "$folder" && $tstamp -lt $(stat --print "%Y" "$folder") ]]; 然后
    after=$(ls "$folder" | tr '\n' '\n' > /tmp/after.txt)


    差异1=$(comm -2 -3 "/tmp/after.txt" "/tmp/prev.txt">/tmp/Diff.txt)
    添加=$(cat /boot/common/cache/tmp/Diff.txt)


    差异2=$(comm -2 -3 "/tmp/prev.txt" "/tmp/after.txt">/tmp/Diff2.txt)
    丢失=$(cat /boot/common/cache/tmp/Diff2.txt)


    difffile1=/tmp/Diff.txt
    difffile2=/tmp/Diff2.txt
    FILESIZE2=$(stat -c%s "$difffile2")
    如果 [ "$FILESIZE2" == 0 ]
    然后
    丢失=没有
    菲
    FILESIZE1=$(stat -c%s "$difffile1")
    如果 [ "$FILESIZE1" == 0 ]
    然后
    添加=无

    菲

    lost2=$(cat /boot/common/cache/tmp/Diff2.txt)
    警报 --idea "$文件夹:

    $已添加
    *插入*。

    $丢失
    *删除*。";

    echo "$lost2" >>$folder/Removed.txt

    tstamp=$(stat --print "%Y" "$folder")

    猫 /dev/null > $difffile1
    猫 /dev/null > $difffile2
        别的                                                                                                                                                                                            
            睡觉 3;                                                                                                                                                                                    
        菲                                                                                                                                                                                              
    完毕

4

3 回答 3

1

为什么不能使用inotifywaitinotifywatch?您可以使用-r标志使其递归。看看inotify-tools
您可能也对incron感兴趣。

于 2013-09-03T15:51:18.993 回答
1

我最终进行了自己的修改,但是这个可以工作并且不再使用临时文件。它使用findprocess 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
于 2013-09-03T16:30:00.687 回答
0

你想增加这一行:

prev=$(ls "$folder" | tr '\n' '\n'  > /tmp/prev.txt)

类似于:

prev=$(ls "$folder"/* | tr '\n' '\n'  > /tmp/prev.txt)
于 2013-09-03T15:35:43.563 回答