PDA

View Full Version : For Loop Issue



pjpalmq
September 27th, 2007, 08:09 PM
Greetings!

I have a routine I use to convert wav files to mp3 files.

for i in *.wav; do
lame -h -b 192 "$i" "${i%.wav}.mp3"
rm "$i"
done

This works great one directory at a time. I would like to start at my top level directory and have this routine work recursively.

Any ideas?

Many Thanks!
Peter

gnusci
September 27th, 2007, 08:23 PM
You can use:

> find . -type d

to get the directories and then just use each directory.

pjpalmq
September 27th, 2007, 09:48 PM
Please forgive me.....how would that look? I understand your command returns all directories from this point down, but how would I incorporate this into my little script?

Thanks so much for the help!

Best,
Peter

yabbadabbadont
September 27th, 2007, 09:52 PM
while read i
do
lame -h -b 192 "$i" "${i%.wav}.mp3"
rm "$i"
done < find /path/to/root/dir/of/music -type f -name "*.wav"

:D

(test it on a small test directory tree first)

pjpalmq
September 27th, 2007, 10:11 PM
Okay, I tried this....but as you can see I get a syntax error.

$ while read i
> do
> lame -h -b 192 "$i" "${i%.wav}.mp3"
> rm "$i"
> done < find /home/pjpalmq/Music -type f -name "*.wav"
bash: syntax error near unexpected token `/home/pjpalmq/Music'

Thanks!
Still Trying!
:confused:

yabbadabbadont
September 27th, 2007, 10:16 PM
Sorry. That's why I told you to test it first. :)

Try this instead:

find /path/to/root/dir/of/music -type f -name "*.wav" | while read i
do
lame -h -b 192 "$i" "${i%.wav}.mp3"
rm "$i"
done


I tested it this time. ;)


/home/daffy/temp $ mkdir -p jj/kk/ll
/home/daffy/temp $ touch jj/jj.wav jj/kk/kk.wav jj/kk/ll/ll.wav
/home/daffy/temp $ find . -type f -name "*.wav" | while read i
> do
> echo $i
> done
./jj/jj.wav
./jj/kk/ll/ll.wav
./jj/kk/kk.wav

pjpalmq
September 27th, 2007, 10:30 PM
WOW!!!! This is fan-friggin-tastic! Thanks so much!

:popcorn:

It will run for hours, but it is so much better than me sitting here babysitting!

Thanks again!
Best,
Peter

yabbadabbadont
September 27th, 2007, 10:40 PM
Perhaps you should have changed it to check the error code returned by lame before removing the source file... especially since it will be running unattended for hours. ;)

pjpalmq
September 27th, 2007, 10:50 PM
Okay, enlighten me? While I do have the originals all saved on a DVD, what could happen?

yabbadabbadont
September 27th, 2007, 10:54 PM
Lame could, for some reason (out of disk space, ...), fail to create the mp3 file. Yet the script will unconditionally remove the wav file anyway. Since you have the original files backed up, I wouldn't worry about it.

pjpalmq
September 27th, 2007, 10:57 PM
Good to know. Thankfully, that won't be an issue.

Out of curiosity, what would the error look like?

Best,
Peter

yabbadabbadont
September 27th, 2007, 11:17 PM
/home/daffy $ lame jj
Could not find "jj".
/home/daffy $ echo $?
1

The return code would be zero if there were no errors.

Edit: So you would do something like this:

find /path/to/root/dir/of/music -type f -name "*.wav" | while read i
do
lame -h -b 192 "$i" "${i%.wav}.mp3"
if [ $? -eq 0 ]
then
rm "$i"
fi
done

pjpalmq
September 28th, 2007, 12:16 AM
Thank-you! That make total sense! :popcorn: