I need to concatenate four binary, meaning non-text files. Lets say I have four files:


Now I want my resulting file to look like: ABCDABCDABCDABCD. So I want to alternate every byte. I would also like to be able to alternate every n bytes meaning alternate every 1,2,3,4,etc bytes.

So far, most of the information I searched for deals with text files and uses cat & paste which appears to only work with lines of text. I also cant find any data on alternating concatenation for more than two files.

  • I doubt a ready-made tool exists to do this, but it's probably fairly easy to cobble together a shell script to do it. Are all the files of the exact same size? – a CVn Aug 23 '15 at 17:52
  • @MichaelKjörling Yes, The files are the same size. – Mister Tea Aug 23 '15 at 20:44
up vote 1 down vote accepted

This should work. Set each to the number of bytes to read each time.

len=$(stat -c %s file1)
while [ $len -gt 0 ]
 dd bs=$each count=1 <&5
 dd bs=$each count=1 <&6
 dd bs=$each count=1 <&7
 dd bs=$each count=1 <&8
 let len=len-$each
done 5<file1 6<file2 7<file3 8<file4 2>/dev/null

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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