Skip to content

Instantly share code, notes, and snippets.

@Garbee
Created October 30, 2018 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garbee/bcaa0b53380ce8575567180931efdc10 to your computer and use it in GitHub Desktop.
Save Garbee/bcaa0b53380ce8575567180931efdc10 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
pathToMove="$1"
if [ ! -d "$pathToMove" ]; then
echo 'Path does not exist'
exit 1
fi
function moveFile() {
sourceFile="$1"
metadata=$(exiftool "$sourceFile")
targetPath="/Volumes/DATA/Music"
year=$(echo "$metadata" | awk -F': ' '/^Original Release Year/ {print $2}')
title=$(echo "$metadata" | awk -F': ' '/^Title/ {print $2}'| tr "/" "~")
album=$(echo "$metadata" | awk -F': ' '/^Album / {print $2}' | tr "/" "~")
albumArtist=$(echo "$metadata" | awk -F': ' '/^Band/ {print $2}' | tr "/" "~")
track=$(echo "$metadata" | grep ^Track | awk -F': ' '{print $2}' | awk -F'/' '{print $1}')
totalTracks=$(echo "$metadata" | grep ^Track | awk -F': ' '{print $2}' | awk -F'/' '{print $2}')
extension=$(echo "$metadata" | awk -F': ' '/^File Type Extension/ {print $2}')
disc=$(echo "$metadata" | awk -F': ' '/Part Of Set/ {print $2}' | awk -F'/' '{print $1}')
totalDiscs=$(echo "$metadata" | awk -F': ' '/Part Of Set/ {print $2}' | awk -F'/' '{print $2}')
organizationalFolder=$(echo "$albumArtist" | cut -c1-1)
if [ -z "$albumArtist" ]; then
destination="${targetPath}/misc/${album}/${title}.${extension}"
destinationPath=$(dirname "${destination}")
mkdir -p "$destinationPath"
mv -n "${sourceFile}" "$destination"
sourcePath=$(dirname "$sourceFile")
rmdir -p "$sourcePath" 2> /dev/null
exit 0
fi
while [ ${#track} -lt ${#totalTracks} ]; do
track="0$track"
done
destination="${targetPath}/"
if [ "Various Artists" = "${albumArtist}" ]; then
destination+="[Various Artists]/"
if [ -z "$year" ]; then
destination+="Unknown Release Year/"
else
destination+="${year}/"
fi
else
destination+="${organizationalFolder}/${albumArtist}/"
if [ ! -z "$year" ]; then
destination+="[${year}] "
fi
fi
destination+=${album}/
if [ ! -z "$totalDiscs" ]; then
if [ "$totalDiscs" -gt "1" ]; then
destination+="${disc}-"
fi
fi
destination+="${track} - ${title}.${extension}"
destinationPath=$(dirname "$destination")
mkdir -p "$destinationPath"
mv -n "${sourceFile}" "${destination}"
sourcePath=$(dirname "$sourceFile")
rmdir -p "$sourcePath" 2> /dev/null
}
files=$(find "${pathToMove%/}" -type f -name '*.mp3')
totalFiles=$(echo "$files" | wc -l)
count=0
IFS=$'\n'
echo -e "${count} / ${totalFiles} \r\c"
for file in $files; do
count=$((count + 1))
echo -e "${count} / ${totalFiles} \r\c"
moveFile "$file"
done
IFS=$' \t\n'
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment