3

I am using the following command

 mkvmerge -o  newfile.mkv  1.mkv  +2.mkv  +3.mkv   +4.mkv  etc...

this command collates the video files in the list. Usually I have this files in a folder, the number of files can change (not always 4 as in the example). I would like to get a command that:

  1. take the list of mkv files in the given dir
  2. run the command above.

Basically I know how to get the list, eg. FileList=$(ls *mkv)), but then I don't how to get the "+" in front of the file names escluding the first in the list.

Can somebody help with this?

Regards Pierre

3
LIST=''
for i in *.mkv; do
    if [ -n "$LIST" ]; then LIST="$LIST +"; fi
    LIST="$LIST$i"
done

mkvmerge -o newfile.mkv $LIST

Test if the list is empty; if not, you need to add a space and +. Then run the command with the generated list.

  • 1
    Instead if.. construction you can use bash variable expansion LIST=${LIST:+$LIST' +'}$i – Costas Feb 6 '15 at 16:00
1

With bash

shopt -s nullglob
shopt -s extglob
first_file=1.mkv
add_files=(!(${first_file%.mkv}).mkv)
printf "%s\n" "${first_file}" "${add_files[@]/#/+}"
1.mkv
+2.mkv
+3.mkv
+4.mkv
mkvmerge -o newfile.mkv "${first_file}" "${add_files[@]/#/+}"
1

This method does not use a loop.
Also, to allow for embedded non-printable characters (eg. space), ls -Q quotes the filename

eval "$(ls -Q1 *.mkv | sed '1s/^/mkvmerge -o newfile.mkv /; 1!s/^/ +/' | tr -d '\n')"

Note that tr acts at the byte level, and as such will delete \x0D (newline char) embedded within non-ascii filename chars.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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