LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Entering folders recursively to run script on files nested inside, ubuntu 7.10 (https://www.linuxquestions.org/questions/linux-newbie-8/entering-folders-recursively-to-run-script-on-files-nested-inside-ubuntu-7-10-a-598115/)

aidansmoker 11-08-2007 01:27 PM

Entering folders recursively to run script on files nested inside, ubuntu 7.10
 
Hi,

I'm pretty new to Linux but have some grasp of basics. I wanted to convert about 10 thousand files in .wma to .mp3 and found a script to do it which is shown below. Problem is it will only convert the files in one folder. How can I get it to operate recursively on a series of nested folders so that it enters thm automatically and I don't have to cd into each one and run by hand?

Thanks in advance

Script: courtesy user from melbourne, aus., username=cajd
*********************************************************

#!/bin/bash

current_directory=$( pwd )

#remove spaces
for i in *.wma; do mv "$i" `echo $i | tr ' ' '_'`; done

#remove uppercase
for i in *.[Ww][Mm][Aa]; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done

#Rip with Mplayer / encode with LAME
for i in *.wma ; do mplayer -vo null -vc dummy -af resample=44100 -ao pcm -waveheader $i && lame -m s audiodump.wav -o "`basename "$i" .wma`.mp3"; done

#convert file names
for i in *.wma; do mv "$i" "`basename "$i" .wma`.mp3"; done

rm audiodump.wav

rnturn 11-08-2007 03:38 PM

Quote:

Originally Posted by aidansmoker (Post 2952433)
wanted to convert about 10 thousand files in .wma to .mp3 and found a script to do it which is shown below.

Assuming your script is called "wmaconv", what about:
Code:

find top-level-dir -type d | while read DIRNAME
do
  cd $DIRNAME
  wmaconv
done

If you modified your script so that it would accept the directory name as a command line argument and did the "cd" for you, you could then do the above as a one-liner:
Code:

find top-level-dir -type d -exec wmaconf {} \;
Hope this helps a bit.

--
RT

aidansmoker 11-09-2007 11:28 AM

Spaces
 
Thanks RT

One last problem though, how do I modify your code to deal with spaces in the folder names; the code seems to be having problems with that?
It gives me "Music/The: no such file or dir." error for folder 'The X'.

Aidan

rnturn 11-11-2007 10:43 PM

Quote:

Originally Posted by aidansmoker (Post 2953469)
... how do I modify your code to deal with spaces in the folder names; the code seems to be having problems with that?

OK... If you're referring to the "while-do" example, I'm guessing that the "cd $DIRNAME" line is where the error lies, right? Try changing that line to read:
Code:

cd "$DIRNAME"
Single quotes should not be used because the shell substitution would break. If you're trying to get into one of these directories from the CLI, you can precede each space by a backslash ("\") or enclose the space-laden directory/file name in quotes:
Code:

cd Djam\ Karet
cd "Djam Karet"
cd 'Djam Karet'

would all work.
Single or double quotes will work in this case so long as you aren't referencing a shell variable:
Code:

cd '$GENRE/Djam Karet'
would not work (see the first code snippet's warning). Double quotes would be needed in the above example.

Boy! What I'd give for five minutes in a dark alley with a baseball bat and the bozo who thought spaces in directory and file names was a cool idea. I probably get at least one question a week at work about this. It's gotten to be second nature for me when saving files sent to me -- almost always from a Windows user but from the occasional Mac user as well -- that I automatically change all the spaces to underscores. Making that little fix once saves me so much trouble later on. Fixing them -- or just dealing with them -- after the fact is a pain. As you're finding out. Unfortunately, I seem to recall some ripping software that seemed to want to embed spaces in sound filenames. Guess that's why I wound up sticking with cdrecord and homegrown scripts. (And guess what my scripts do with any spaces in album/song titles? :) )

aidansmoker 11-11-2007 11:39 PM

Thats it alright, thanks RT. I agree, I prefer to stick to no spaces or capitals, it's just all round easier.


All times are GMT -5. The time now is 11:43 PM.