我来到这个帖子(监视文件夹,如果其中有文件则运行命令?) 监控文件夹,然后对找到的文件执行操作。一切正常,但现在我想在移动文件时添加“.tmp”扩展名,然后再次删除 .tmp 扩展名。但是,它不是先移动到 .tmp 文件,而是将文件移回 .mkv 文件。
假设我这么做:
cp /home/j-j/test/move_script/tv/tv_s01e01.mkv /home/j-j/test/move_script/transcoded_tv/
- tv_s01e01.mkv 正在复制到 transcoded_tv
- 同时 tv_s01e01.mkv 被 inotifywait 识别
- 文件首先被复制到 watched_tv 作为 tv_s01e01.mkv,而不是 tv_s01e01.mkv.tmp。
解决方案可能很简单,但找不到解决方案。有谁能帮我吗?下面您可以找到脚本。
#!/bin/bash
## set path to watch
movies="/home/j-j/test/move_script/transcoded_movies"
tv="/home/j-j/test/move_script/transcoded_tv"
## set path to copy the script to
watchedMovies="/home/j-j/test/move_script/watched_movies"
watchedTv="/home/j-j/test/move_script/watched_tv"
## Inotify Monitor
inotifywait -m -r -e moved_to -e create "$movies" "$tv" --format "%f" | while read f
## Inotify Daemon
# inotifywait -d -r -e moved_to -e create "$movies" "$tv" --format "%f" | while read f
do
echo $f
shopt -s nocasematch
## check if file is a tv show
if [[ $f == *S[0-9][0-9]E[0-9][0-9]* ]] ; then
mediaType=$tv
watchedDir=$watchedTv
## file is a movie
else
mediaType=$movies
watchedDir=$watchedMovies
fi
## check if the file is not a cache file and is a .mkv file
if [[ $f != *TdarrCacheFile* ]] && [[ $f = *.mkv ]] ; then
## creating temporary file first and then revert to mkv
mv "$mediaType/$f" "$watchedDir/$f.tmp" && mv "$watchedDir/$f.tmp" "$watchedDir/$f"
## and rum it
/bin/bash "$watchedDir/$f" &
fi
done
答案1
文件被复制到
watched_tv as tv_s01e01.mkv
而不是tv_s01e01.mkv.tmp
首先复制到。
不,它tv_s01e01.mkv.tmp
首先被移动为,然后重命名为tv_s01e01.mkv
即刻... 要查看它的实际效果,请使用标志运行您的脚本-x
,这将启用调试/跟踪脚本的执行,如下所示:
bash -x scriptfile
评论:
答案2
最终脚本:
#!/bin/bash
## set path to watch
movies="/home/j-j/test/move_script/transcoded_movies"
tv="/home/j-j/test/move_script/transcoded_tv"
## set path to copy the script to
watchedMovies="/home/j-j/test/move_script/watched_movies"
watchedTv="/home/j-j/test/move_script/watched_tv"
## Inotify Monitor
inotifywait -m -r -e close_write "$movies" "$tv" --format "%f" | while read -r f
## Inotify Daemon
# inotifywait -d -r -e close_write "$movies" "$tv" --format "%f" | while read -r f
do
echo "$f"
shopt -s nocasematch
if [[ $f == *S[0-9][0-9]E[0-9][0-9]* ]] ; then
mediaType=$tv
watchedDir=$watchedTv
else
mediaType=$movies
watchedDir=$watchedMovies
fi
## check if the file is a .mkv file
if [[ $f != *TdarrCacheFile* ]] && [[ $f = *.mkv ]] ; then
# if so, move the file to the target dir
mv "$mediaType/$f" "$watchedDir"
fi
done