0

我正在尝试复制保存到 torrent 保存文件夹的已完成文件。我使用 inotifywait 创建了一个 bash 脚本来监视该文件夹。我的原始脚本非常适合文件和文件夹/子文件夹。一旦我用“-e close_write”替换了“-e create -emoved_to”,它就不再识别保存在监视文件夹中的文件夹。它现在仅适用于文件。我需要更改为这个的原因是因为我的很多文件都很大(并且文件夹中的多个文件)并且需要一些时间才能完全保存并且脚本在完成之前运行所有进程。

我也尝试过“等待”命令,但它什么也没做。我知道有更复杂/更好/正确的使用方法,但我还没有弄清楚。

这是我的脚本示例。任何指导将不胜感激。我不擅长编写 bash 脚本,如果写得一团糟,我很抱歉。

WATCHED=/mnt/Seeding
DESTINATION=/mnt/Complete-Torrents
user=userid
group=groupid
perms=777

# Step 1 - Copying completed torrents to Complete-Torrents folder leaving original torrents to seed 
inotifywait -m -e close_write --format %f $WATCHED \
    | while read new
        do
            echo Detected new torrent $new, Copying to Complete-Torrents folder
            cp -r "$WATCHED/$new" "$DESTINATION/"
            wait
                          
# Step 2 - Deleting unwanted files from Complete-Torrents folder

            echo Deleting unwanted files from Complete-Torrents folder
            find $DESTINATION -type f -iname "*.txt" -delete
            find $DESTINATION -type f -iname "*.nfo" -delete
            find $DESTINATION -type f -iname "*.website" -delete
            find $DESTINATION -type f -iname "*.exe" -delete
            find $DESTINATION -type f -iname "*.html" -delete
            find $DESTINATION -type f -iname "*.htm" -delete
            find $DESTINATION -type f -iname "*.sfv" -delete
            find $DESTINATION -type f -iname "*.parts" -delete
            find $DESTINATION -type f -iname "*.jpg" -delete
            find $DESTINATION -type f -iname "*.png" -delete
            find $DESTINATION -type f -iname "*.doc" -delete
            sleep 10
                           
# Step 3 - Change permissions of new files in Complete-Torrents folder

            echo Changing ownership and permissions to Complete-Torrents folder and files
            chown -R $user:$group "$DESTINATION"
            chmod -R $perms "$DESTINATION"
                           
        done

谢谢,肖恩

编辑...

我一生都无法获得此脚本,甚至无法看到添加到监视文件夹中的新文件夹。它实际上什么也没做。如果我close_writecreate&替换moved_to它并且不做其他更改,它会正确地看到文件夹和内容并处理。我觉得这很奇怪。

事实上,我什至尝试制作一个小的测试脚本,看看我是否可以让它与 if/elif/else 语句一起工作,但是当我再次在监视文件夹中复制一个文件夹时,脚本什么也不做(甚至不做到循环)。如果我放入一个文件,那么它会提供正确的输出。

这是我运行的测试脚本。其他人可以确认他们是否可以正确识别和处理新文件夹吗?

#!/bin/bash
WATCHED=/mnt/Watched
inotifywait -re close_write --format '%w%f' -m $STEP1_WATCHED \
    | while read -r new
        do
            if [[ -d "$new" ]]; then            
            echo "Detected new folder $new"
            elif [[ -f "$new" ]]; then            
            echo "Detected new file $new"
            else
            echo "neither $new"
            fi
            done
    done
4

1 回答 1

0

It turns out that the issue here was my mistake. In all my times reading through the inotify manual I somehow did not catch that this only works for files and not folders. I obviously was looking for something specific and glossed over what the manual was saying about close_write.

Thanks to all that helped!

于 2020-12-15T21:29:45.770 回答