Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a folder full of .wav audio files.

Some are stereo, most are mono splits.

The mono split pairs are all named foo bar track.L.wav and foo bar track.R.wav

I can use the command line tool sox to combine a mono pair into 1 stereo track like this:

sox -M track1.L.wav track1.R.wav track1.Stereo.wav

where the first 2 files are the mono pairs, and the third is the output stereo file.

This is great, but I'd like to have a script that will automatically find all the mono pairs and combine them into stereo files.

I.e., I need it to find all files which have the same name except for the .L. and .R. before the extension, and run sox on them, outputting to a new file with the same name without the L/R suffix.

For example, if my folder contains these files:


I need to run these commands:

sox -M track1.L.wav track1.R.wav track1.Stereo.wav
sox -M track2.L.wav track2.R.wav track2.Stereo.wav
sox -M track3.L.wav track3.R.wav track3.Stereo.wav
sox -M track4.L.wav track4.R.wav track4.Stereo.wav

Here's where I am so far:

for file in ./*.L.wav;
    file2=`echo $file | sed 's_\(.*\).L.wav_\1.R.wav_'`;
    out=`echo $file | sed 's_\(.*\).L.wav_\1.STEREO.wav_'`;
    echo $file - $file2 - $out;

That works, but when I replace the echo line with

sox -M $file $file2 $out;

it doesn't work; spaces in the filenames cause it to fail.

share|improve this question

Try with this from a GNU/Linux console:

for file in ./*L*wav;
    for file2 in ./*R*wav;
        $out = "Stereo"
        sox -M $file $file2 $file-$out.wav;

NOTE: "./" represent the music directory where your files are.

share|improve this answer
sox needs 3 args: the 2 files, plus the output file. I need the output file name to be the name of the first 2 without the L or R – Ze'ev Oct 4 '12 at 22:52
Sorry, this does not work; it does not match up the matching files. It just returns every file with an "L", followed by every other file, recursively. – Ze'ev Oct 4 '12 at 22:56
Yeah, I am sorry; I forgot the third argument – slackmart Oct 4 '12 at 22:58
I updated the post to clarify; your code still doesn't line up matching pairs. Also, ./*.L.wav works better... – Ze'ev Oct 4 '12 at 23:07
The second for statement needs to somehow find files that match the * from the first for – Ze'ev Oct 4 '12 at 23:09
up vote 2 down vote accepted

Going from sgmart's suggestion, I got it solved:

for file1 in ./*.L.wav; do 
  file2=`echo $file1 | sed 's_\(.*\).L.wav_\1.R.wav_'`;
  out=`echo $file1 | sed 's_\(.*\).L.wav_\1.STEREO.wav_'`;
  sox -MS "$file1" "$file2" "$out";
  mv "$file1" mono; mv "$file2" mono;

Needed "'s around the variables.

And I added a bit to move the processed files to a 'mono' subfolder.

share|improve this answer

For people that needs to do this in Windows with a batch file:

The 4 rules that need to be fullfilled are:

1) The batch file must be put in the directory where SOX is installed

2) Mono files needs to have .L.WAV and .R.WAV terminations

3) No white spaces in the file name

4) Put all your mono files/folders in a new folder called input inside the SOX installation folder

After running the batch file you'll get a new folder called output with all the stereo files and the same folder structure as the input folder

The code is commented so it is easy to follow each step in the process, feel free to modify and post here improved versions of this script (The copy part should be a move but I'm not very expert in cmd batch code!).

Here we go:

cd %~dp0
setlocal enabledelayedexpansion

REM for each file with extension .L.WAV in input folder and its subdirectories do:
for /R %~dp0input %%F in (*.L.WAV) do (

    REM get fullpath name and then remove the last 6 characters. ex: MYWAVE.L.WAV -> MYWAVE
    set fileName=%%F
    set finalName=!filename:~0,-6!

    REM set names for each parameter used by sox
    set wavL=!finalName!.L.WAV
    set wavR=!finalName!.R.WAV
    set wavST=!finalName!.ST.WAV

    REM call sox for mono to stereo conversion
    sox !wavL! !wavR! !wavST! -MS


REM creat output folder
mkdir %~dp0output

REM copy files to output folder 
echo d | xcopy %~dp0input %~dp0output /S

REM delete ST files from input folder 
for /R %~dp0input %%F in (*.ST.WAV) do del %%F

REM delete L files from output folder 
for /R %~dp0output %%F in (*.L.WAV) do del %%F

REM delete R files from output folder 
for /R %~dp0output %%F in (*.R.WAV) do del %%F
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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