help using find/xargs to apply mp3gain to files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help using find/xargs to apply mp3gain to files
# 1  
Old 02-29-2012
help using find/xargs to apply mp3gain to files

I need to apply mp3gain (album mode) to all mp3 files in a given directory. Each album is in its own directory under /media/data/music/albums for example:

Code:
/media/data/music/albums/foo
/media/data/music/albums/bar
/media/data/music/albums/more

What needs to happen is:
Code:
cd /media/data/music/albums/foo && mp3gain -a -k -p *.mp3
cd /media/data/music/albums/bar && mp3gain -a -k -p *.mp3
cd /media/data/music/albums/more && mp3gain -a -k -p *.mp3

I want to use find and xargs to do this but am unsure how to craft the right syntax. I have used the this construct to accomplish tasks like this before, but this won't do both the cd into the dir and then run the mp3gain command.

Code:
find /media/data/music/album -type d -print0 | xargs -0 cd {} && mp3gain -a -k -p *.mp3

Thanks!
# 2  
Old 02-29-2012
Why not just pipe it into a while:

Code:
find /path -type d | while read dir
do
    cd $dir &&  command *.mp3
done

# 3  
Old 02-29-2012
To ensure mp3gain process .mp3 files only in those directories that contain mp3 files, the first while loop + sort -u will single out the these directories. The second while loop will then cd into each of the directory to run mp3gain. I use sub-shell to cd to the directory so that I do not have to 'cd ..' back.
Code:
find /media/data/music -type f -name "*.mp3" | 
while read f
do
  echo ${f%/*}
done | sort -u | while read d
do 
  (cd $d && mp3gain -a -k -p *.mp3)
done

# 4  
Old 02-29-2012
Similarly, but assuming no knowledge of subdirectories and paranoia about empty directories or filenames containing spaces:

Code:
ls -1d /media/data/music/albums/* 2>/dev/null | while read dir
do
        if [ -d "${dir}" ]
        then
               cd "${dir}"
               ls -1d *.mp3 2>/dev/null | while read filename
               do
                           if [ -f "${filename}" ]
                           then
                                 mp3gain -a -k -p "${filename}"
                           fi
               done
        fi
done

# 5  
Old 02-29-2012
Nice, thanks for the suggestions!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fast processing(mv command) of 1 million+ files using find, mv and xargs

Hi, I'd like to ask if anybody can help improve my code to move 1 million+ files from a directory to another: find /source/dir -name file* -type f | xargs -I '{}' mv {} /destination/dir I learned this line of code from this forum as well and it works fine. However, file movement is kinda... (6 Replies)
Discussion started by: agentgrecko
6 Replies

2. Shell Programming and Scripting

Help with find, xargs and awk

Hi, I want to find some files and then search for some lines in it with a particular pattern and then write those lines into a file. To do this I am using something like this from command prompt directly. cd /mdat/BVG find -name "stmt.*cl" -newer temp.txt | xargs -i awk '/BVG-/{print}' {} >... (7 Replies)
Discussion started by: Sandhya Harsh
7 Replies

3. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

4. Shell Programming and Scripting

Xargs + Find Help

Guys i want to run a command to list all directories that havn't been modified in over 548 days ( 1.5 yrs ). Id like to run a script to first print what the command finds ( so i get a list of the files pre move ... i have a script set for this : find /Path/Of\ Target/Directory/ -type d -mtime... (4 Replies)
Discussion started by: modulartention
4 Replies

5. Shell Programming and Scripting

Apply 'awk' to all files in a directory or individual files from a command line

Hi All, I am using the awk command to replace ',' by '\t' (tabs) in a csv file. I would like to apply this to all .csv files in a directory and create .txt files with the tabs. How would I do this in a script? I have the following script called "csvtabs": awk 'BEGIN { FS... (4 Replies)
Discussion started by: ScKaSx
4 Replies

6. Shell Programming and Scripting

find and xargs

hi, i've been trying to figure this weird error but I cannot seem to know why. I am using below find command: find . \( ! -name . -prune \) -type f -mtime +365 -print The above code returns no file because no files are really more then 365 days old. However, when I use xargs, its... (9 Replies)
Discussion started by: The One
9 Replies

7. Shell Programming and Scripting

find with xargs to rm found files

I believe what is happening is rm is executing in the script on every directory and on failure of the first it stops although returns status 0. find $HOME -name /directory/filename | xargs -l rm This is the code I use but file remains. I am using sun solaris system which has way limited... (4 Replies)
Discussion started by: Ebodee
4 Replies

8. UNIX for Dummies Questions & Answers

XARGS and FIND together

I am trying to delete files older than 60 days from a folder: find /myfolder/*.dat -mtime +60 -exec rm {} \; ERROR - argument list too long: find I can't just give the folder name, as there are some files that I don't want to delete. So i need to give with the pattern (*.dat). I can... (3 Replies)
Discussion started by: risshanth
3 Replies

9. Shell Programming and Scripting

Script to find/apply Solaris 10 ACL's

This may be a question for a different forum, but as I will need a script I thought I would start here. We recently migrated from Solaris 8 to Solaris 10. The file system in question here is ZFS, meaning the method for listing and applying ACL's has changed dramatically. To make a long story... (3 Replies)
Discussion started by: Shoeless_Mike
3 Replies

10. Linux

How to find and apply patches in RHAT linux?

Hi: This is an elementary qs. Thanks in advance, (4 Replies)
Discussion started by: bbose
4 Replies
Login or Register to Ask a Question