Bash: Scan a folder for files by extension.

This version just eliminates all of the output for folders that do not contain m4a files to convert. This is version 2.01 of m4aTOmp3 that will recurse all folders under the default or specified folder name. It replaces spaces with “_”, coverts uppercase to lowercase AND replaces all “(” & “)” with “-” in an attempt to make all files playable using “play”.

Save it as the text file m4aTOmp3 in your home area ~/bin folder (/home/name/bin).

#!/bin/bash

#: Title       : /home/james/bin/m4aTOmp3
#: Date Created: Sun Dec 5 09:09:11 CST 2010
#: Last Edit   : Sun Dec 5 16:59:11 CST 2010
#: Author      : J. McDaniel
#: Version     : 2.01
#: Description : Convert m4a files to mp3 / recurses ALL folders under default
#: Options     : m4aTOmp3 [folder]

# Created for openSUSE forums on Sunday December 5, 2010

# To use this script file, copy all of the text within into a text file
# called m4aTOmp3 in your home area bin folder. (~/bin/m4aTOmp3)
# To mark executable, open a terminal session and run the command: chmod +x ~/bin/m4aTOmp3
# To Use m4aTOmp3 open a Terminal session and type in either m4aTOmp3 in the folder
# To begin your serach for files to convert OR type m4aTOmp3 folder where
# folder is the name of the folder to start your *.m4a serach for songs to convert to *.mp3
# m4aTOmp3 recurses all folder names at or after the default or specified folder to convert.

# Change to the specified folder $1 if requested
if  "$1" != "" ] ; then
 cd $1 > /dev/null 2>&1
fi

# Save Default folder location as all Sub Folders are in relation to this location
startin=$( pwd )
counter=0

#Locate ALL subfolders from the present startfolder
for folder in $(find . -type d) ; do

# Change to $startin Folder and then to Sub $Folder as all folders are relative to $Startin
  cd $startin
  cd $folder > /dev/null 2>&1

# If the folder contains any m4a files then convert them
  if (ls *.[Mm]4[Aa] > /dev/null 2>&1) ; then
    let counter=counter+1

#
# This section contains the modifications to files before the conversion
#

#replace all spaces with an underline 
    for i in *.[Mm]4[Aa] ; do
      mv "$i" `echo $i | tr ' ' '_'`
    done

#replace all uppercase letters with lowercase
    for i in *.[Mm]4[Aa] ; do 
      mv "$i" `echo $i | tr '[A-Z]' '[a-z]'` 
    done

#replace all ( letters with -
    for i in *.[Mm]4[Aa] ; do 
      mv "$i" `echo $i | tr '(' '-'` 
    done

#replace all ) letters with -
    for i in *.[Mm]4[Aa] ; do 
      mv "$i" `echo $i | tr ')' '-'` 
    done

#
# With new filenames, m4a files are converted to wav and then to mp3 files
#

# Convert m4a to wav files first
    for i in *.m4a ; do 
      faad $i 
    done

# The convert wav to mp3 last
    for i in *.wav ; do 
      name="${i%\.*}.mp3" 
      lame -b 128 $i $name  
    done 

# Removing all old files
    rm *.wav
    # rm *.m4a
  fi

done

echo
echo "The following Number of Folders were Converted for You: $counter"
echo

exit 0
# End Of Script

To make it executable, run the following command:

chmod +x ~/bin/m4aTOmp3

To use m4aTOmp3 open a terminal session and type:

m4aTOmp3 [folder]

m4aTOmp3 recurses all folder names started from the default or from the specified folder name. Once a folder with m4a files are found it does the following:

  1. Converts all spaces to underscore
  2. Converts all uppercase to lowercase
  3. Replaces all “(” or “)” with a dash
  4. Converts all *.m4a files to *.wav
  5. Converts all *.wav to *.mp3
  6. Removes all *.wav files
  7. Removes all *.m4a files if the remark has been removed
  8. Find next folder with *.m4a files and repeat

Please, I need comments from anyone that uses this file as to if it works for them. I spent some time trying to make this work properly.

Thank You,