LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-14-2006, 12:19 AM   #1
General
Member
 
Registered: Aug 2005
Distribution: Debian 6.0
Posts: 465

Rep: Reputation: 31
Run script on every file contained within a parent directory and its subdirectories


I am trying to perform the same task on all files within a folder. In the following bash script, wav files in each directory are converted to mp3 format.

Code:
#!/bin/sh

cd ~/Music/Artist1/Album1
for i in *.wav
do lame -h -b 192 "$i" "`basename "$i" .wav`.mp3"
done

cd ~/Music/Artist2/Album1
for i in *.wav
do lame -h -b 192 "$i" "`basename "$i" .wav`.mp3"
done

cd ~/Music/Artist2/Album2
for i in *.wav
do lame -h -b 192 "$i" "`basename "$i" .wav`.mp3"
done

etc.
This script goes on quite a ways. I can use what I have, but as I frequently end up writing scripts that have to perform tasks on hundreds of files, and my scripts take a long time to write up in this way, I am wondering if there is a better way to have it perform this task on each file contained within the ~/Music directory.
 
Old 05-14-2006, 01:08 AM   #2
finklewicz
Member
 
Registered: Mar 2005
Posts: 41

Rep: Reputation: 15
Try the etree-scripts at http://etree-scripts.sourceforge.net/ . It's got a bunch of commands for converting different file types, including to mp3,flac and ogg. I recommend flac and ogg of course, as they're open source. I believe you can use the 'shn2mp3' command on your wav's and it'll convert 'em for you.
It's got lots of cool features, including the ability to parse an info text file and use the data, like songlists, to tag your music files.
 
Old 05-14-2006, 01:45 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
You don't need to cd to each subdirectory, you could either loop through a list of subdirectories or use the find command to locate all .wav files.

For example:
Code:
for file in $(find ./ -iname "*.wav"); do
   lame -h "${file}" "${file%wav}mp3"
done

or

for file in subdir1 subdir2 subdir3; do
   for file in "$subdir"/*.wav; do
      lame -h "$file" "${file%wav}mp3"
   done
done
The variable expansion ${file%wav} strips the 'wav' characters from the filename. The directory part of the filename is present in both in the first example.

You could produce a simple script that does the conversion, such as:
#!/bin/bash
lame -h "$1" "${1%wav}mp3"

Suppose you name this program "wav2mp3" and save it in ~/bin/. Make sure it is executable: "chmod +x ~/bin/wav2mp3".

Now, this oneliner will convert all of the WAV files to MP3s:
find ./ -iname "*.wav" -execdir wav2mp3 '{}' \;

Last edited by jschiwal; 05-15-2006 at 03:11 AM.
 
Old 05-14-2006, 10:18 PM   #4
burninGpi
Member
 
Registered: Mar 2006
Location: Fort McMurray, Canada
Distribution: Gentoo ~amd64
Posts: 163

Rep: Reputation: 30
Quote:
Originally Posted by jschiwal
Make sure it is executable: "chmod ~/bin/wav2mp3".
I think you mean
Code:
chmod +x ~/bin/wav2mp3
 
Old 05-15-2006, 03:12 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Thumbs up

Thank you for the correction. I edited my previous post.

Regards.
 
  


Reply

Tags
lame


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 08:42 PM
monitoring file added to a directory / subdirectories abhi_abhijith Programming 4 02-13-2006 08:25 AM
Run script during file copy or create in directory neranjana Linux - General 1 01-13-2004 07:57 AM
How do I run a command in script from a particular directory? davee Linux - Newbie 1 08-07-2003 04:22 AM
mv the contents of one directory to the parent directory warkrime Linux - Newbie 4 07-14-2003 08:03 PM


All times are GMT -5. The time now is 11:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration