I'm currently trying to write a little script to convert all the flac files to mp3 files. However, I ran into a bit of a problem when trying to set up recursion down into all my music folders - the script kept looping into the current directory (.)
Here's what I currently have:
#!/bin/bash
#---
# flacToMp3: Converts FLAC files in my originalFLAC folder into mp3 files
# and places them in an identical folder structure in my Music
# folder.
#---
function enterDIR {
for DIR in "$(find . -maxdepth 1 -type d)"; do #recurse into every directory below top-level directory
if [ "$DIR" == "." ]; then #avoid current directory infinite loop
continue
fi
cd "$DIR/"
enterDIR
done
createDirectory
convertFLAC
}
function createDirectory {
#recreate directory structure in Music folder
curDir="$pwd"
newDir=${curDir/originalFLAC/Music}
mkdir -p $newDir
}
function convertFLAC {
#convert each flac file in current directory into an mp3 file
for FILE in "$(find . -maxdepth 1 -type f)"; do #loop through all regular (non-directory) files in current directory
if [ "${FILE: -5}" == ".flac" ]; then #if FILE has extension .flac
ffmpeg -i "$FILE" -ab 320k -map_metadata 0 "${FILE%.*}.mp3"; #convert to .mp3
mv -u "${FILE%.*}.mp3" $newDir
else #copy all other files to new directory as-is
cp -ur "$FILE" $newDir
fi
done
}
enterDIR
This script is pretty clunky, since I only just started dipping into Bash. The problem (or at least where I think it is) comes from the if [ "$DIR" == "." ]; then
line - looking at my output when running the script, it doesn't seem to filter it.
How do I filter out (ignore) the current directory?
find . -maxdepth 1 -name "*.flac"
?for
to iterate over lines.find . -type f -name "*.flac"
would suffice. Let find delve into your directories.