Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Toss away music that's already converted with iTunes. Flexible, file extension is a variable, locations are variables.
#!/bin/bash
# CHANGE THESE!!
# This is the folder where your music is located (*all* music within this folder is handled)
DIRECTORY="/path/to/your/music"
# this is the safe (temporary?) location for your files.
NEW_PLACE="/path/to/the/destination/folder"
ORG_EXT='.m4a'
CONVERTED_EXT='.mp3'
# Change this to 1 to get more output.
VERBOSE=0
if [ -z "$todo" ]; then
todo=0
conv=0
fi
find $DIRECTORY -type f -name "*$ORG_EXT" | while read FILE; do
echo "----------------------"
BASE=$(basename "$FILE" $ORG_EXT)
if [ "$VERBOSE" != "0" ]; then echo " ..base is '$BASE'"; fi
SUBDIR=$(dirname "$FILE")
if [ "$VERBOSE" != "0" ]; then echo " ..SUBdir is '$SUBDIR'"; fi
NEWDIR=${SUBDIR/$DIRECTORY/$NEW_PLACE}
if [ "$VERBOSE" != "0" ]; then echo " ..NEW dir is '$NEWDIR'"; fi
echo -n "Processing $FILE ..."
if [ "${BASE:0:1}" != "." ]; then
if [ "$VERBOSE" != "0" ] && [ -f "$SUBDIR/$BASE$ORG_EXT" ]; then
echo -n " .... ORG_EXT found"
fi
if [ "$VERBOSE" != "0" ] && [ -f "$SUBDIR/$BASE$CONVERTED_EXT" ] ; then
echo -n ' .... $CONVERTED_EXT found'
fi
if [ "$VERBOSE" != "0" ]; then echo " .... lookup for '$SUBDIR/$BASE$CONVERTED_EXT' and '$SUBDIR/$BASE$ORG_EXT' done --- continue." ; fi
if [ -f "$SUBDIR/$BASE$CONVERTED_EXT" ] && [ -f "$SUBDIR/$BASE$ORG_EXT" ]; then
if [ "$VERBOSE" != "0" ]; then echo "VERIFY $NEWDIR ..."; fi
if [ ! -d "$NEWDIR" ]; then
echo -n " ... creating dir $NEWDIR"
mkdir -p "$NEWDIR" && echo ' ...created' || echo ' ...failed!'
fi
conv=$(( $conv + 1 ));
echo -n " ==> Moving $conv... to '$NEWDIR/$BASE$ORG_EXT'"
mv "$SUBDIR/$BASE.m4a" "$NEWDIR/$BASE$ORG_EXT" && echo " ==> Done." || echo " ==> FAILED!!"
else
todo=$(( $todo + 1 ));
echo " ==> SKIPPING $todo, file not yet converted."
fi
else
echo -n " ==> SKIPPING, filename starts with a dot."
echo " ==> remove the file"
rm "$SUBDIR/$BASE$ORG_EXT"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment