I have many (7500) .wav files (named by their numbers) which I'd like to merge into one. I think it has to be done with a bash loop because with using globbing some files would be in the wrong order (like 1111 before 2).

What's the best way to do this under Linux?

2 Answers 2


If all the files have the same parameters, like sample rate and number of channels, you still can't just catenate them. You have to strip away the WAV header.

It may be easiest to use an audio file manipulation utility like sox, which contains a method for catenating files. In fact, it does this by default. E.g. to combine three .wav files into one long one:

$ sox short1.wav short2.wav short3.wav long.wav

A loop will not arrange the files in the order you want. What you want is to sort the names, but treat them numerically. sort -n will do this.


$ sox $(ls *.wav | sort -n) out.wav

If sox cannot handle that many files, we can break up the job like this:

$ ls *.wav | sort -n > script

Then we have a script file which looks like:


We edit that to make several command lines:

# catenate about half the files to temp1.wav   
sox \
1.wav \
2.wav \
... \
3999.wav \

# catenate the remainder to temp2.wav  
sox \
4000.wav \
... \
7500.wav \

# catenate the two halves
sox temp1.wav temp2.wav out.wav ; rm temp1.wav temp2.wav

As the first editing step on the file list, you can use the vi command :%s/.*/& \\/ to add a backslash after every line.

  • Thanks,this seems to be the right way, but it seems like sox doesn't take more than 6479.wav files (quits with "sox FAIL formats: can't open input file `6479.wav': Too many open files") :-(
    – Neula
    Commented Mar 26, 2013 at 20:05
  • Ah, in that case what we can do is simply break the operation down. For instance, we can divide the list of files into two halves, merge each half and then merge the halves. I would capture the output of ls *.wav | sort -n into a file, turn the list into sox command lines and then source it.
    – Kaz
    Commented Mar 26, 2013 at 20:16
  • 1
    Keeping all the files open simultaneously looks like a bug/deficiency in sox.
    – Kaz
    Commented Mar 26, 2013 at 20:22
  • In the end it showed up that sox doesnt'take more than 348 files so I would have to do this 21 times. When merging the first files I noticed that they seem to be damaged, so merging them doesn't help me. Anyway, you solved the problem! thank you
    – Neula
    Commented Mar 26, 2013 at 20:38
  • You mean the original files are damaged? That's too bad.
    – Kaz
    Commented Mar 26, 2013 at 20:50

This has been bugging me today as well as I had many more files to concatenate than sox can load. So I ended up using this shell script inside the folder with the wavs (please make sure you don't need in.wav and out.wav as they'll be overwritten):

rm in.wav out.wav                                                                                                                            
ls *.wav | sort -n | while read l;
   if [ ! -f in.wav ]                                                                                                                                         
      cp $l in.wav                                                                                                                                            
      sox in.wav $l out.wav                                                                                                                                   
      cp out.wav in.wav                                                                                                                                       
   echo "$l"                                                                                                                                                  

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .