Bash: Scan a folder for files by extension.

Well, I’m hopeless at bash (and every other programming language), and need to convert a whole lot of m4a files to mp3. How would I go about scanning my Music folder (and sub folders) for m4a files, then getting a console audio converter to convert them. The new files would have the same name as the original, of course, and if possible, could the old files be deleted? Thanks.

I found this one online. It converts only the files in the default folder while in a terminal session. Save the text file as m4atomp3 in your ~/bin folder (/home/name/bin):

#!/bin/bash  

for i in *.m4a; do     

  echo "Converting: ${i%.m4a}.mp3"     

  faad -o - "$i" | lame - "${i%.m4a}.mp3" 

done

To make the file executable, open a terminal session and type:

chmod +x ~/bin/m4atomp3

This program does not yet search for sub folders and it does not remove the old files. See if it works at all and we can add on as required. This one requires faad & lame to be installed.

Thank You,

To do the same recursively, you can use

for i in $(find . -name "*.m4a" ) ; do

It gave me some mp3 files, but they weighed 834 bytes and only made a small pop when played. But then this isn’t a problem with your script, so do I just need to find different converters to use in place of faad and lame?

Anyways, here’s the output for one of the songs:

 *********** Ahead Software MPEG-4 AAC Decoder V2.7 ******************

 Build: Oct 20 2010
 Copyright 2002-2004: Ahead Software AG
 http://www.audiocoding.com
 Floating point version

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License.

 **************************************************************************

Megadeth - Hangar 18 - Rust In Peace [Bonus Tracks].m4a file info:

LC AAC  310.960 secs, 2 ch, 44100 Hz

title: Hangar 18
artist: Megadeth
writer: Dave Mustaine
album: Rust In Peace [Bonus Tracks]
track: 2
totaltracks: 13
disc: 1
totaldiscs: 1
date: 1990
compilation: 
unknown: 
tempo: 00000 BPM
tool: iTunes 8.0.2.20, QuickTime 7.5.5
iTunSMPB:  00000000 00000840 000002B0 0000000000D16910 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
Encoding Params: vers
iTunNORM:  00002BDD 00002662 0000E420 0000C184 000180FB 000180FB 00007E89 00007E89 000000E6 000000E6
iTunes_CDDB_IDs: 13+3F8DDB6003C432E8BD2A22AF31493CC6+4330971
UFIDhttp://www.cddb.com/id3/taginfo1.html: 3CD3N96R62335982V7812E9D8158C0593E5CEC40F835A794DFFP1
album_artist: Megadeth
genre: Metal
cover: ����

  ---------------------
 | Config:  2 Ch       |
  ---------------------
 | Ch |    Position    |
  ---------------------
 | 00 | Left front     |
 | 01 | Right front    |
  ---------------------

LAME 3.98.4 32bits (http://www.mp3dev.org/)Peace [Bonus Tracks].m4a.
CPU features: MMX (ASM used), SSE (ASM used), SSE2
Using polyphase lowpass filter, transition band: 16538 Hz - 17071 Hz
Encoding <stdin> to Megadeth - Hangar 18 - Rust In Peace [Bonus Tracks].mp3
Encoding as 44.1 kHz j-stereo MPEG-1 Layer III (11x) 128 kbps qval=3
Converting: Megadeth - Holy Wars...The Punishment Due - Rust In Peace [Bonus Tracks].mp3
 *********** Ahead Software MPEG-4 AAC Decoder V2.7 ******************

 Build: Oct 20 2010
 Copyright 2002-2004: Ahead Software AG
 http://www.audiocoding.com
 Floating point version

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License.

 **************************************************************************

Megadeth - Holy Wars...The Punishment Due - Rust In Peace [Bonus Tracks].m4a file info:

LC AAC  392.266 secs, 2 ch, 44100 Hz

title: Holy Wars...The Punishment Due
artist: Megadeth
writer: Dave Mustaine
album: Rust In Peace [Bonus Tracks]
track: 1
totaltracks: 13
disc: 1
totaldiscs: 1
date: 1990
compilation: 
unknown: 
tempo: 00000 BPM
tool: iTunes 8.0.2.20, QuickTime 7.5.5
iTunSMPB:  00000000 00000840 00000044 0000000001082F7C 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
Encoding Params: vers
iTunNORM:  0000216A 00002221 0000B4E8 00012AE2 000595AF 00035EAE 00007E89 00007E89 000019E0 00001A3C
iTunes_CDDB_IDs: 13+3F8DDB6003C432E8BD2A22AF31493CC6+4330971
UFIDhttp://www.cddb.com/id3/taginfo1.html: 3CD3N93R62335981U208F05E87ED87DA99E9D1ADD0ABF5B811FBP7
album_artist: Megadeth
genre: Metal
cover: ����

  ---------------------
 | Config:  2 Ch       |
  ---------------------
 | Ch |    Position    |
  ---------------------
 | 00 | Left front     |
 | 01 | Right front    |
  ---------------------

LAME 3.98.4 32bits (http://www.mp3dev.org/)shment Due - Rust In Peace [Bonus Tracks].m4a.
CPU features: MMX (ASM used), SSE (ASM used), SSE2
Using polyphase lowpass filter, transition band: 16538 Hz - 17071 Hz
Encoding <stdin>
      to Megadeth - Holy Wars...The Punishment Due - Rust In Peace [Bonus Tracks].mp3
Encoding as 44.1 kHz j-stereo MPEG-1 Layer III (11x) 128 kbps qval=3

Oh, and @jdmcdaniel3, thank you.

On 2010-12-05 07:06, matACCADACCA wrote:
>
> It gave me some mp3 files, but they weighed 834 bytes and only made a
> small pop when played. But then this isn’t a problem with your script,
> so do I just need to find different converters to use in place of faad
> and lame?

There are dozens of options for lame, please see the manual.

Perhaps you have to try manually first, without the script, to find the
best combination for a single file.


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” at Telcontar)

I actualy fouind some m4a files in my collection and then converted them to MP3 and they did play fine. Here is the resulting script file. Save it as the 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 09:09:11 CST 2010
#: Author      : J. McDaniel
#: Version     : 1.00
#: Description : Convert m4a files to mp3 
#: Options     : none

current_directory=$( pwd )

#remove spaces

for i in *.[Mm]4[Aa]; do 

  mv "$i" `echo $i | tr ' ' '_'`; 

done

#remove uppercase

for i in *.[Mm]4[Aa]; do 

  mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; 

done


#convert m4a to wav files

for i in *.m4a ; do 

  faad $i; 

done

# convert wav to mp3

for i in *.wav ; do 

  name="${i%\.*}.mp3" ;
  lame -b 128 $i $name ; 

done 

# removing old files
rm *.wav
# rm *.m4a

exit 0
# End Of Script

To make it executable, run the following command:

chmod +x ~/bin/m4aTOmp3

To use m4aTOmp3 open a terminal session, change to the folder containing the m4a files and type in m4aTOmp3 and watch it go. It seems that you need to get rid of spaces and capital letters for proper conversion in Linux. Further, the file faad program converts everything to wav first, then uses lame to convert it to mp3. Notice the lame bitrate option of 128, this is the default, but you may want a higher number. You can test the mp3 files with the terminal program called play name.mp3 (part of the application called sox).

If this does not work for you, then you have multimedia problems. I suggest you download and run my script called mmcheck. Message #40 has the most recent version:

MMCHECK - Check Your Multimedia in 10 Steps - Script File, as proposed by RedDwarf

Thank You,

OK, here is version 2.00 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:16:11 CST 2010
#: Author      : J. McDaniel
#: Version     : 2.00
#: 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
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

# If the folder contains any m4a files then convert them
  if (ls *.[Mm]4[Aa]) ; 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,

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,

Hint: you can get tr to do multiple substitutions in one command.

Also have a look at bash’s builtin parameter substitution ability, which can take the place of the echo and tr, reducing the process invocations.

Hint: you can get tr to do multiple substitutions in one command.

Also have a look at bash’s builtin parameter substitution ability, which can take the place of the echo and tr, reducing the process invocations.

Hello ken_yap, I will do as you suggest, but I am thinking it may be tomorrow before I do so. As it would seem I have blown my house work day today getting just this version to work right. I have learned a lot I must say already and making it shorter over understanding was not my hope. But I know just as you say there is a better way to do this.

Thank You,

Thank you! This script works an absolute charm! I really do appreciate the time and effort you have put in. I’ve tried it on a test folder - now it’s time to do it on the whole Music folder. See you on the other side.

Thank you! This script works an absolute charm! I really do appreciate the time and effort you have put in. I’ve tried it on a test folder - now it’s time to do it on the whole Music folder. See you on the other side.
You are very welcome matACCADACCA. It was a learning experience for me, looking up the many commands and writing it to work as you requested. I would always suggest you backup your music files before you let lose a program that can remove files. Good luck and I hope it does just what you needed to do.

Thank You,

HI,
You like complicated things guys…
Dolphin: CTRL + F > seach field: *.m4a (check subfolders)
A list will be generated. select all files found by CTRL+A , click right on one of them> actions>convert to (choose format). it’s done!

HI,
You like complicated things guys…
Dolphin: CTRL + F > seach field: *.m4a (check subfolders)
A list will be generated. select all files found by CTRL+A , click right on one of them> actions>convert to (choose format). it’s done!
phiga, why would you want to do things the easy way for? If you want to learn bash, I am thinking you need to duplicate every useful thing you can think of. Anyway, this is Linux, right? It supposed to be hard. lol! And thanks for the info, I will check it out later when no one is looking.

Thank You,

You’re assuming that the rest of the world is using KDE. (What about twm users?)