This solution was tested on /bin/bash from a GNU/Linux system. I'm assuming your mac has a /bin/bash for this, along with awk, tr, grep, ls, rm, and wc. (/bin/bash is the default shell executable). This script also assumes that you have ONLY mp3 and m4a files, and that some directories contain no mp3 and m4a files. (wrote it on the
Again, this is assuming that Mac has bash, tr, grep, ls, rm, and wc (standard UNIX utilities, iirc)
(sorry for bad scripting practices, I'm not very good at shell scripting)
(directory = folder)
(Specify the destination directory using the FIRST ARGUMENT) (i.e. ./thisscript.sh directoryname)
(before you do this, read through the whole post)
Create a text file in the parent directory of all those directories with the music in them and paste this in:
Now navigate to that directory in the shell and type:
Note that you can't do something as complicated like this without a script. ;)
Walking you through it:
Tells us to use /bin/bash, the shell.
Parse the first argument as the destination directory and store it in $todir (a variable).
cd $( dirname $0 )
Just to make things safe so we can execute the script from any directory, as long as the script is in the same directory as all the directories of the music stuff.
DIRLIST=$( ls -F | grep '/' | tr -d '/' )
List the directories.
for DIR in $DIRLIST
Iterate through the directories.
mp3num=$( ls $DIR | grep -c \\.mp3 )
m4anum=$( ls $DIR | grep -c \\.m4a )
filnum=$( ls $DIR | wc -l )
Store some variables we need (number of mp3's, number of m4a's, number of files
echo $DIR $mp3num $mp4num $filnum
(for debugging only)
if [ $DIR = $todir ]; then
If the directory we are looking at on the current iteration, skip it.
if [ $m4anum = 0 -o $mp3num = 0 ]
If the directory has no m4a's or no mp3's, skip it.
elif [ $mp3num != 0 -a $m4anum != 0 ]
mv $DIR/*.mp3 $todir
If the directory has some mp3's and some m4a's, then move all the mp3's in the directory to the specified destination directory.
The reason why this will probably work on macs as well is because macs are unix-based. They also have bash, which is the shell language we're using here, and all those little unix utilities (I looked them up and they appeared as the first result in Mac Manpages.)
Also, make sure that the destination directory EXISTS, or it won't work...