Skip to content

Instantly share code, notes, and snippets.

@Programie
Created August 25, 2014 00:00
Show Gist options
  • Save Programie/5db9a82e2f265adc5eb8 to your computer and use it in GitHub Desktop.
Save Programie/5db9a82e2f265adc5eb8 to your computer and use it in GitHub Desktop.
Reorganise music from ID3 tags
#! /bin/bash
BASEPATH="/data/music" # Replace with your music directory
for FILE in $BASEPATH/*.mp3; do
ID3_INFO="`id3info \"$FILE\" | grep -i \"===\"`"
TITLE=$(echo "$ID3_INFO" | grep -i TIT2)
ARTIST=$(echo "$ID3_INFO" | grep -i TPE1)
ALBUM=$(echo "$ID3_INFO" | grep -i TALB)
YEAR=$(echo "$ID3_INFO" | grep -i TYER)
TRACK=$(echo "$ID3_INFO" | grep -i TRCK)
TITLE=${TITLE##*: }
ARTIST=${ARTIST##*: }
ALBUM=${ALBUM##*: }
YEAR=${YEAR##*: }
TRACK=${TRACK##*: }
if [ -z "$TITLE" ]; then
echo "Missing title tag: $FILE"
continue
fi
if [ -z "$ARTIST" ]; then
echo "Missing artist tag: $FILE"
fi
if [ -z "$ALBUM" ]; then
echo "Missing album tag: $FILE"
continue
fi
# You can change the file structure template here
TARGETDIRECTORY="$BASEPATH/$ALBUM"
TARGETFILE="$TARGETDIRECTORY/$ARTIST - $TITLE.mp3"
echo "$FILE -> $TARGETFILE"
mkdir -p "$TARGETDIRECTORY"
if [ -f "$TARGETFILE" ]; then
echo "File already exists: $TARGETFILE"
continue
fi
mv "$FILE" "$TARGETFILE"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment