使用 bash 脚本移动文件

Jak*_*ber 5 permissions command-line bash ftp scripts

我有一个监视文件夹的 bash 脚本,如果将某些内容添加到文件夹中,脚本会将受监视文件夹中的所有内容移动到定义的目的地。

问题: - 该脚本无法通过 FileZilla (FTP) 将受监控文件夹中的文件移动到目标文件夹。如果您通过 shell 提示手动将文件移动到受监视的文件夹,该脚本确实有效。FTP 是唯一无法正常工作的格式。

任何想法可能是什么问题?

这是脚本:

inotifywait -m ~/folderA/fileA -e moved_to |
    while read path action file; do
        #echo "The file '$file' appeared in directory '$path' via '$action'"
        # do something with the file
    mv ~/folderA/fileA/* "/folderB/myNewDest"
    done
Run Code Online (Sandbox Code Playgroud)

wal*_*tor 6

首先,通过inotifywait -e moved_to只监视移动到目标目录的文件,您可以省略监视在那里写入覆盖的文件,例如Filezilla. 添加-e modify -e create到您的命令中,或者,除非您有令人信服的理由忽略某些inotifywait事件,否则请放弃所有-e whatever选项。

其次,通过不在mv ~/folderA/fileA/* "/folderB/myNewDest"命令中引用 from 文件,您可能会被愚蠢的文件名捕获,例如foo;rm -rf *. 我会建议

find ~/folderA/fileA/ -maxdepth 0 -type f -print0 | \
xargs -0 mv --target-directory=/folderB/myNewDest --
Run Code Online (Sandbox Code Playgroud)

它做同样的事情,但更安全。