sorting output of find before running the command in -exec
<p>I have a series of directories containing multiple mp3 files with filenames 001.mp3, 002.mp3, ..., 030.mp3.</p>

<p>What I want to do is to put them all together in order into a single mp3 file and add some meta data to that.</p>

<p>Here's what I have at the moment (removed some variable definitions, for clarity):</p>

<pre><code>#!/bin/bash
for d in */; do
    cd $d
    find . -iname '*.mp3' -exec lame --decode '{}' - ';' | lame --tt "$title_prefix$name" --ty "${name:5}" --ta "$artist" --tl "$album" -b 64 - $final_path"${d%/}".mp3
    cd ..
done
</code></pre>

<p>Sometimes this works and I get a single file with all the "tracks" in the correct order.</p>

<p>However, more often than not I get a single file with all the "tracks" in reverse order, which really isn't good.</p>

<p>What I can't understand is why the order varies between different runs of the script as all the directories contain the same set of filenames.  I've poured over the man page and can't find a sort option for find.</p>

<p>I could run <code>find . -iname '*.mp3' | sort -n &gt;&gt; temp.txt</code> to put the files in a temporary file and then try and loop through that, but I can't get that to work with lame.</p>

<p>Is there any way I can put a sort in before find runs the exec?  I can find plenty of examples here and elsewhere of doing this with <code>-exec ls</code> but not where one needs to execute something more complicated with exec.</p>