Register
It is currently Fri Nov 28, 2014 3:33 pm

Directory tree "travelling" problem


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Thu Dec 04, 2008 6:43 am   

Joined: Thu Dec 04, 2008 6:22 am
Posts: 3
Hello. As you can see, I'm a newbie here, and I need some help with the bash scripting... so I figured this would be the best place to ask.

Anyway, I'm trying to make a script that would recursively travel through the directory tree (starting with the current dir, from where it was run), and do something in that dir.
For example, I'd like to run through my entire MP3 music collection, and scan the contents of each directory with mp3gain as if each dir was an album. This means I have to cd into each directory (or something like that), and mp3gain all the .mp3 files in it as an album.

What I've come up with until now is this:

Code:
for i in "$(find . -type d)";
do
   mp3gain -o -a -k -w "$i"/*.mp3
done


However, this doesn't do much, since it just says it can't open any *.mp3 for reading.
Can someone please help?

Many thanks.


Top
 Profile WWW ICQ  
 PostPosted: Thu Dec 04, 2008 7:18 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
why not just do it like this:
Code:
#!/bin/bash
for i in $(find . -iname \*.mp3); do
    mp3gain -o -a -k -w "$i"
done


Would do the same thing but give you full paths to your mp3 files :P
(If a directory or a file contains a space character this could fail)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Thu Dec 04, 2008 12:50 pm   

Joined: Thu Dec 04, 2008 6:22 am
Posts: 3
Yes, this fails when directories have spaces in their names. Any ideas how to fix that?

and btw, the reason why I'm not doing this is the -a switch, which scans files as an album. So there's a rather large difference whether I scan a single directory (and I have directories separated by album names) as an album and the whole collection as one album.


Top
 Profile WWW ICQ  
 PostPosted: Thu Dec 04, 2008 1:16 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
There are several ways, but i prefer to muck around with IFS variable.
Code:
#!/bin/bash
IFS="\n"
for i in ....


The IFS variable decides what should be an itteration, the default value of this is a space character :)

(just don't set it in your shell and forget about it, running it in a script will reset the variable when the script is done)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Thu Dec 04, 2008 3:11 pm   

Joined: Thu Dec 04, 2008 6:22 am
Posts: 3
Alright, so I wrote it like this:

Code:
#!/bin/bash
IFS="\n"
for i in "$(find . -type d)";
do
   mp3gain -o -a -k -w "$i"/*.mp3
done


but the shell doesn't see the files. Any other suggestions? Is there something else I could use, besides find? Did I do something wrong?
Thanks for your effort.


Top
 Profile WWW ICQ  
 PostPosted: Fri Dec 05, 2008 3:52 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Code:
find . -type f -iname \*.mp3 -exec mp3gain -o -a -k -w "{}" \;


Top
 Profile  
 PostPosted: Fri Dec 05, 2008 3:57 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
I was about to write that one :P But my work got in the way :)

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP