Shell脚本移动具有相同名称的文件

时间:2013-04-04 21:22:41

标签: shell

我想制作一个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脚本,因此感谢您的帮助。

1 个答案:

答案 0 :(得分: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

详细了解%。*此处: Extract filename and extension in Bash