-2

我想制作一个定期运行的 shell 脚本来检查和移动我的视频文件(如果它们有关联的字幕)

有点像这样:

sourceDir = "/volume/mount/Videos";
destinationDir = "/volume/mount/VideosWithSubtitles";
validExtensions = ["avi", "mkv", "mp4"]

for every file in sourceDir that has a "validExtension"
check if a file with the same name but with a ".srt" extension exists
if it does, move both of them to "destinationDir"

我不熟悉 shell 脚本,因此感谢您的帮助。

4

1 回答 1

0

快速而肮脏,改变以满足您的需求

destinationDir=/tmp/dest
sourceDir=/tmp/src
set -x
cd $sourceDir
for i in *.mp4 *.mkv *.avi; do  
    if [ -f "${i%.*}.srt" ]; then 
        mv $i $destinationDir
        mv ${i%.*}.srt $destinationDir
    fi
done

set +x

输出:

hvn@lappy: /tmp () $ bash myscript.sh
+ cd /tmp/src
+ for i in '*.mp4' '*.mkv' '*.avi'
+ '[' -f ghk.srt ']'
+ for i in '*.mp4' '*.mkv' '*.avi'
+ '[' -f xyz.srt ']'
+ for i in '*.mp4' '*.mkv' '*.avi'
+ '[' -f abc.srt ']'
+ mv abc.avi /tmp/dest
+ mv abc.srt /tmp/dest
+ set +x

在此处阅读有关 %.* 的更多信息: 在 Bash 中提取文件名和扩展名

于 2013-04-05T09:20:42.597 回答