mvmp3 (Source)

#!/bin/bash
##---------------------------------------------------------------------------##
#
#   Script: mvmp3
#   Author: Brian <genius@groupbcl.ca> :)
#   Date:   April 2001
#   
#   Update: April 2007 to handle multiple files
#   Update: November 2007 to allow for keeping the original filenames
#   Update: January 2016 to show each file name only once, along with a
#           number that's used to look up the original name
#
#   Assist with the renaming of MP3 files, which typically have very long
#   file names with embedded spaces.
#   
##---------------------------------------------------------------------------##
# BUUS: This script is part of Brian's Useful Utilities Set

function die {
    echo "mvmp3: $1"
    [ -f $TEMP1_FN ] && rm -f $TEMP1_FN
    exit 1
}

[ -z "$1" ] && die "require the name of a file"

TEMP1_FN="/tmp/mvmp3.list.$$.A.txt"
TEMP2_FN="/tmp/mvmp3.list.$$.B.txt"
declare -a ORIGINAL_FN

# Gather the original file names
FN_COUNT=0
for FN in "$@"
do
    [ ! -f "$FN" -a ! -d "$FN" ] && die "\"$FN\": not found"
    [ ! -w "$FN" ] && die "\"$FN\": cannot rename: read-only"
    FN_COUNT=$((FN_COUNT+1))
    ORIGINAL_FN[$FN_COUNT]="$FN"
done

# Set up the temporary file for the user to edit
M=0
for FN in "${ORIGINAL_FN[@]}"
do
    echo -e "$FN\t[$((++N))]" >>$TEMP2_FN
done
column -t -s$'\t' $TEMP2_FN >$TEMP1_FN
rm -f $TEMP2_FN

# Let the user edit the file
[ -z "$EDITOR" ] && EDITOR=vim
$EDITOR $TEMP1_FN

# Perform the renames
sed 's/[[:space:]]*\[\([0-9]*\)\]$/\t\1/' $TEMP1_FN |
  awk -F'\t' '{print "TO_FN=\"" $1 "\" FILE_NUM=" $2}' |
  while read LINE
do
    eval "$LINE"
    FROM_FN="${ORIGINAL_FN[$FILE_NUM]}"

    if [ "$FROM_FN" -a "$TO_FN" ]
    then
        if [ "$FROM_FN" = "$TO_FN" ]
        then
            echo "* Not renaming $FROM_FN"
        else
            echo "* Rename \"$FROM_FN\" to \"$TO_FN\""
            [ -f "$TO_FN" ] && die "! Cannot rename; \"$TO_FN\" already exists"
            mv "$FROM_FN" "$TO_FN"
        fi
        FROM_FN='' TO_FN=''
    fi
done

rm $TEMP1_FN

# vim: tabstop=4