I'm trying to adding subtitles for multiple mkv files. I use this script for doing this :

for file in *.mkv; do "/Applications/Mkvtoolnix-7.0.0.app/Contents/MacOS/mkvmerge" -o "${file%\.*}(1).mkv"   "(" "$file" ")"  "(" "${file%\.*}.srt" ")" ; rm $file

It work fine for root directory only. But it don't work for all files in subfolder , please help me to make it work for all files in subfolder.

2 Answers 2


You could do something like this:

# make bash function to do conversion and deletion
conv() {
 /Applications/Mkvtoolnix-7.0.0.app/Contents/MacOS/mkvmerge -o "${1%\.*}(1).mkv"   "(" "$1" ")"  "(" "${1%\.*}.srt" ")"
 rm "${1}"
# make conv function available to xargs
export -f conv
# recursively convert every .mkv file in current directory
find . -name "*.mkv" -type f -print0 | xargs -0 -n1 -I% bash -c 'conv "$@"' _ %

If you're using bash, do

shopt -s globstar
for file in **/*.mkv
    (your MKVmerge, etc., commands)

Setting the globstar shell option tells bash to expand **/ as all directories and subdirectories.

P.S. Always quote all shell variable references (unless you have a very good reason not to, and you're sure you know what you're doing).  In particular, say rm "$file" instead of rm $file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.