Code:
for x in *.wav ; do sox $x ${x%.*}.ogg && rm $x; done
Don't forget to quote the variables, to keep filenames with spaces (and other reserved characters) from breaking the command.
And to set this up as a stand-alone script:
Code:
#!/bin/bash
# A simple set of loops that convert wav's to ogg files
# The input arguments are directories to work in:
for dir ; do
(
cd "$dir"
echo "processing directory $PWD"
for x in *.wav ; do
sox "$x" "${x%.*}.ogg" && rm "$x"
done
)
done
exit 0
Note how I put the inner block inside
(...) brackets. This runs them inside a
subshell, and so you don't need to
cd back out or anything at the end. When the subshell exits, the environment automatically reverts back to that of the parent.
bash from version 4.0 also has a new "
globstar" recursive pattern matching option. To run the script on a directory and all its subdirectories, you can do this:
Code:
shopt -s globstar #needs to be enabled first
./scriptname /var/spool/asterisk/monitor/**/
Alternately you could modify the script like this:
Code:
#!/bin/bash
# A simple set of loops that convert wav's to ogg
# files recursively from a given top directory.
# The input parameter is the directory to start from:
# This script requires bash 4.0+
shopt -s globstar
for dir in "$1/"**/ ; do
(
cd "$dir"
echo "processing directory $PWD"
for x in *.wav ; do
sox "$x" "${x%.*}.ogg" && rm "$x"
done
)
done
exit 0