Last active
September 13, 2016 23:18
-
-
Save dellow/dd378c1937eeb32e0955d66c89baea8a to your computer and use it in GitHub Desktop.
Music Files Sorter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# musicsorter.sh | |
# | |
# About: | |
# Will sort a directory of music files by recognising the track name and sorting them into directories like Artist > Track Name.ext. | |
# | |
# How To Use: | |
# `bash musicsorter.sh ./path/to/directory` | |
# | |
# Check if a directory has been supplied. | |
if [ -z "$1" ] ; then | |
echo "You must supply a directory." | |
exit 0 | |
# Check directory exists. | |
elif [ ! -d "$1" ] ; then | |
echo "That directory does not exist." | |
exit 0 | |
fi | |
# Array join function. | |
function join_by { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; } | |
# Path of dir. | |
DIR=$1 | |
# Path of files. | |
FILES="$DIR/*" | |
# Loop through all files. | |
for f in $FILES | |
do | |
# Check this is not a directory. | |
if [ ! -d "$f" ] ; then | |
# Output to console. | |
echo -e "---------------------------------------------------------------" | |
# Get filename of file. | |
FILENAME=$(echo "$f" | rev | cut -d"/" -f1 | rev) | |
# Get file extension of file. | |
EXTENSION="${f##*.}" | |
# Output to console. | |
echo -e "$(tput setaf 5)* Processing track: '$FILENAME'$(tput sgr0)" | |
# Get artist name. (2nd command trims). | |
ARTIST=$(echo "$FILENAME" | awk -F- '{ st = index($0,"-");print $1}'); ARTIST=$(echo "${ARTIST}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') | |
# Create array to store track name. | |
ARTISTARRAY=() | |
# Fix track name | |
for WORD in $ARTIST; do | |
if [[ `echo $WORD | tr [:upper:] [:lower:]` = "(feat" ]] ; then | |
# Parse '(feat.' correctly. | |
ARTISTARRAY+=('(feat.') | |
elif [[ `echo $WORD | tr [:upper:] [:lower:]` = "feat" ]] ; then | |
# Parse 'feat.' correctly. | |
ARTISTARRAY+=('feat.') | |
elif [[ `echo $WORD | tr [:upper:] [:lower:]` = "vs" ]] ; then | |
# Parse 'vs.' correctly. | |
ARTISTARRAY+=('vs.') | |
elif [[ $WORD = "Vs" ]] ; then | |
# Parse 'vs.' correctly. | |
ARTISTARRAY+=('vs.') | |
else | |
# Uppercase first character of word. | |
ARTISTARRAY+=($(tr '[:lower:]' '[:upper:]' <<< ${WORD:0:1})${WORD:1}) | |
fi | |
done | |
# Overwrite ARTIST var with a parsed version. | |
ARTIST=$(join_by ' ' "${ARTISTARRAY[@]}") | |
# Get track name (2nd command removes file extension) (3rd command trims). | |
TRACK=${FILENAME#*-}; TRACK=${TRACK%.*}; TRACK=$(echo "${TRACK}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') | |
# Create array to store track name. | |
TRACKARRAY=() | |
# Fix track name | |
for WORD in $TRACK; do | |
if [[ `echo $WORD | tr [:upper:] [:lower:]` = "(feat" ]] ; then | |
# Parse '(feat.' correctly. | |
TRACKARRAY+=('(feat.') | |
elif [[ `echo $WORD | tr [:upper:] [:lower:]` = "feat" ]] ; then | |
# Parse 'feat.' correctly. | |
TRACKARRAY+=('feat.') | |
elif [[ `echo $WORD | tr [:upper:] [:lower:]` = "vs" ]] ; then | |
# Parse 'vs.' correctly. | |
TRACKARRAY+=('vs.') | |
else | |
# Uppercase first character of word. | |
TRACKARRAY+=($(tr '[:lower:]' '[:upper:]' <<< ${WORD:0:1})${WORD:1}) | |
fi | |
done | |
# Overwrite TRACK var with a parsed version. | |
TRACK=$(join_by ' ' "${TRACKARRAY[@]}") | |
# Create a directory with the artists name. | |
mkdir -p "$DIR/$ARTIST/Singles" | |
# Output to console. | |
echo -e "$(tput setaf 3)* Creating a directory for: '$ARTIST'$(tput sgr0)" | |
# Move this file into the new directory. | |
mv "$DIR/$FILENAME" "$DIR/$ARTIST/Singles" | |
# Output to console. | |
echo -e "$(tput setaf 3)* Moving '$TRACK' to: '$DIR/Singles/$ARTIST'$(tput sgr0)" | |
# Rename track to just the track name. | |
mv "$DIR/$ARTIST/Singles/$FILENAME" "$DIR/$ARTIST/Singles/$TRACK.$EXTENSION" | |
# Output to console. | |
echo -e "$(tput setaf 6)* Track renamed to: '$TRACK.$EXTENSION'$(tput sgr0)" | |
printf "\n" | |
# Output to console. | |
echo -e "$(tput setaf 2)-- TRACK PROCESSED SUCCESSFULLY$(tput sgr0)" | |
# Output to console. | |
echo -e "---------------------------------------------------------------" | |
printf "\n" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment