Skip to content

Instantly share code, notes, and snippets.

@entrity
Last active August 29, 2015 14:15
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 entrity/57777f3963a920946fcd to your computer and use it in GitHub Desktop.
Save entrity/57777f3963a920946fcd to your computer and use it in GitHub Desktop.
Rename media files using data in ID3 tags
#!/bin/bash
# Examines id3 tages of files in given directory and moves them
# to $TOPDIR, renaming path and file to match "artist/album/track - title.ext"
# - if album is absent: "artist/track - title.ext"
# - if track is absent: "artist/album/title.ext"
# - if title is absent: "artist/album/filename"
TOPDIR="$HOME/Music"
for arg in "${@}"; do
case "$arg" in
-t) NO_TITLE_CHANGE=1;;
-d) DRY_RUN=1;;
*) FILE="$arg";;
esac
done
title=`kid3-cli -c 'get title' "$FILE" | awk '{gsub("/","_"); print}'`
if [ -z "$title" ] || [ $NO_TITLE_CHANGE ]; then
name=`basename "$FILE"`
else
ext=`echo "$FILE" | grep -o -P '\.[^.]*$'`
name="$title$ext"
track=`kid3-cli -c 'get track' "$FILE" | grep -o -P '\d+' | head -1`
if [ ! -z "$track" ]; then name=`printf "%02d - $name" "$track"`; fi
fi
artist=`kid3-cli -c 'get artist' "$FILE" | awk '{gsub("/","_"); print}'`
if [ -z "$artist" ]; then echo "ERR: NO ARTIST"; exit 1; fi
album=`kid3-cli -c 'get album' "$FILE" | awk '{gsub("/","_"); print}'`
dir="$artist"
if [ ! -z "$album" ]; then dir="$dir/$album"; fi
dest="$TOPDIR/$dir/$name"
if [ $DRY_RUN ]; then
echo "DRY RUN MOVE TO $dest"
elif [ -e "$dest" ]; then
echo "ERR: FILE EXISTS $dest"
exit 3
else
# move file
echo "mkdir -p \"$TOPDIR/$dir\""
mkdir -p "$TOPDIR/$dir"
echo "mv \"$FILE\" \"$dest\""
mv "$FILE" "$dest"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment