samtools merge can merge bam files while it can not work for sam files.
How to merge sam files on Linux?
Assume the header is from 0.sam, the files to be merged are 0.sam, 1.sam and 2.sam, and the merged output sam file is out.sam.
header=0.sam files="0.sam 1.sam 2.sam" output=out.sam (grep ^@ $header; for f in $files; do grep -v ^@ $f; done) > $output
It should be much faster than the way of “convert sams to bams, merge bams by
samtools merge, convert bam to sam”.