Chaining Matroska files
The mkvtools package provides a way to read and to edit the metadata of any Matroska file without the need to remux directly from the command line. This allows one to write a relatively simple bash script that does the job.

In here the job is broken into three steps:


UID of each file is read by mkvinfo, cleaned, and recorded into the .uid file. At the same time these files are copied to the previous and next file with the extensions .nextuid and .prevuid.
Option file is generated by taking data from .nextuid and .prevuid files where they exist, this option file is saved with .seqtags extension.
The tool mkvpropedit is used to write the information, and the temp files are deleted.


The reason why it is not done in a single step is to allow the user to abort during the read process and to know that no files have yet been altered.

mkvsequence.sh

#!/bin/bash
# GPL3.0
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

FILES=$(ls *.mkv|sort)

echo First reading all the information
for FILE in $FILES
do
    echo Processing $FILE
    exec mkvinfo "${FILE}" | grep "Segment UID: "  | sed "s/^| *+ Segment UID: //g" &gt; "${FILE}.uid"
    if [ $PREVFILE ]
    then
        cp "${FILE}.uid" "${PREVFILE}.nextuid"
        cp "${PREVFILE}.uid" "${FILE}.prevuid"
    fi
    PREVFILE=$FILE
done

echo Creating the necessary option files
for FILE in $FILES
do
    echo Processing $FILE
    if [ -f "${FILE}.prevuid" -o -f "${FILE}.nextuid" ]
    then
        echo $FILE &gt; "${FILE}.seqtags"
        echo --edit &gt;&gt; "${FILE}.seqtags"
        echo info &gt;&gt; "${FILE}.seqtags"

        if [ -f "${FILE}.prevuid" ]
        then
            echo --set &gt;&gt; "${FILE}.seqtags"
            echo prev-uid=$(cat "${FILE}.prevuid") &gt;&gt; "${FILE}.seqtags"
        fi

        if [ -f "${FILE}.nextuid" ]
        then
            echo --set &gt;&gt; "${FILE}.seqtags"
            echo next-uid=$(cat "${FILE}.nextuid") &gt;&gt; "${FILE}.seqtags"
        fi
    fi
done

echo Now writing all the data
for FILE in $FILES
do
    echo Processing $FILE
    mkvpropedit "@${FILE}.seqtags"
    rm "${FILE}.uid" "${FILE}.prevuid" "${FILE}.nextuid" "${FILE}.seqtags"
done

IFS=$SAVEIFS