-   Linux - Newbie (
-   -   error using sox to convert .wav to .raw files (

bloozman23 01-08-2013 12:26 PM

error using sox to convert .wav to .raw files
when i try to use the following command
to find all the .wav files and convert them using sox utility nothing happens.


find `pwd` -name ".wav" -exec sox {} `echo {}| sed 's/.wav/.raw'` \;

colucix 01-09-2013 09:46 AM

The problem is that the command substitution is performed by the shell before the find command actually runs. Therefore the {} symbol is literal and is not substituted with the results of find. To accomplish this you have to run the sox command in a subshell, e.g.

find . -name \*.wav -exec bash -c 'sox "{}" `echo "{}" | sed "s/.wav/.raw/"`' \;
Also notice you missed the closing slash in the sed command. Hope this helps.

David the H. 01-09-2013 04:19 PM

Instead of spawning a bunch of extra shell instances inside -exec, how about just running a loop on the output in the current shell? You can also eliminate sed that way.


while IFS='' read -r -d '' fname; do

        sox "$fname" "${fname%.*}.raw"

done < <( find . -name '*.wav' -print0 )

On another note, instead of running pwd in a subshell, just use the built-in $PWD variable, or just ".".

And $(..) is highly recommended over `..`. Backticks are deprecated syntax.

You might also be able to simply use bash's new globstar feature, for recursive globbing.


shopt -s globstar
for fname in **/*.wav; do

        sox "$fname" "${fname%.*}.raw"


The only danger with it is that if it hits a recursive symlink it will try to follow it, and bog down in an endless loop.

bloozman23 01-10-2013 07:22 AM


archer@archer-HP-Compaq-6710b-RJ459AV:~/Desktop/isolated/data$ time while IFS='' read -r -d '' fname; do  sox "$fname" "${fname%.*}.raw"; done< <( find . -name '*.wav' -print0 )

real        0m1.824s
user        0m0.300s
sys        0m0.532s
archer@archer-HP-Compaq-6710b-RJ459AV:~/Desktop/isolated/data$ time find . -name \*.wav -exec bash -c 'sox "{}" `echo "{}" | sed "s/.wav/.raw/"`' \;

real        0m4.463s
user        0m0.876s
sys        0m1.672s

time third script
real        0m1.707s
user        0m0.400s
sys        0m1.048s

code provided by David and colucix worked fine

All times are GMT -5. The time now is 07:56 AM.