Recursive Prepending Script

As you may recall I prefer to prepend my track names with the album number in the case of multiple disc albums (box sets, double-albums, &c).  I have some rather large (30 CD) sets and am lazy didn’t want to run my other script 30 times, so I wrote this new version to deal with matters recursively.

(There is now an improved version of this script available here. The new version allows the user to select file extensions while the version on the page below is specifically written for FLAC files.)

(Use the pointy brackets button to copy and paste.)

# Prepend album number before track numbers automatically and recursively
# by JamesIsIn from
# Do something nice today.

## gather information

# obtain directory in which to work

echo "This script changes FLAC file names under a containing folder which you supply."
echo "It assumes that your discs are separated into numbered sub-folders."
echo "As such sub-folder names should be 01, 02, and so forth."
echo "Example:  \"01/01 – Track Name.flac\" becomes \"01.01 – Track Name.flac\""

read -e -p "I will require the path to the containing folder: "
   if [ -d "$REPLY" ]; then
      printf "\n\nI have confirmed this is a directory.\n"
      printf "\n\nI cannot find this directory.\n\n"
      echo "$REPLY"
      exit 1

read -p "Press <ENTER> to continue (ctrl-c will abort)."

## do the work

# find all two-digit numerically-named sub-directories under root directory

find "$directory" -type d -name [0-9][0-9] > /tmp/whyme


# parse disc folders into array

declare -a discfind
   let i=0
   while read discline; do

      # extract disc number from folder path
      disc=$( basename "$discline" )
      discpath=$( dirname "$discline" ) 

      # change file names in sub folder

      for filepath in "/$discpath/$disc/"*.[Ff][Ll][Aa][Cc]; do
         filename=$( basename "$filepath" )
         mv "$discpath/$disc/$filename" "$discpath/$disc.$filename"
         echo $filename changed

   done < /tmp/whyme

rm /tmp/whyme



There are a couple of assumptions that this script makes (which are consistent with my preferences).

For instance, I like my file names to run album number dot track number (01.01 – Track Name.flac). As such I name the containing folders each a two-digit number based on the disc number (disc number three goes into a folder called 03). The script then uses that folder name or disc number (since they are now the same) as the prepend for the file name.

I hope that helps to make your life a little better.

Thanks for stopping.


4 thoughts on “Recursive Prepending Script

    1. I agree about automation.

      The problem with the brackets is only in the display on the posts themselves. If you click on the double-bracket symbol in the upper-right corner it will bring up the copy pop-up and there the symbols are displayed correctly. I will look into your link because this replacement is broken and buggy and driving me nuts.

Leave a Reply

Your email address will not be published. Required fields are marked *