Improved Recursive Prepending Script

Not so long ago I wrote a script for prepending disc numbers before song numbers according to folder hierarchies.  You can find the original script here:

Recursive Prepending Script

The original script was designed specifically for working with FLAC files.  I recognize that not everyone has my passion for sound quality or my devotion to this particular open-source format.  As such I have updated that script so that it will accept a user-supplied file extension.  This means that the version of the script you find below may be used for any number of file types (one at a time).

Same as previously, the script assumes that your discs are separated into folders called 01, 02, and so forth.  So you will want to name your sub-folders accordingly (it will ignore folders with names such as Disc 1 or Physical Graffiti 1 or anything really that isn’t a two digit number).

Once you have renamed your disc folders accordingly, this script will begin at a folder you select and parse into that folder, seeking out all folders named with two digit numbers, and renaming all the files contained therein if they have the file extension you supplied.

Also, I like my file names to run album number dot track number (01.01 – Track Name.flac).  You could adjust the script if you prefer a different naming convention.

(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 file names under a containing folder which you supply."
echo "It assumes that your files are separated into numbered sub-folders."
echo "As such sub-folder names should be 01, 02, and so forth (00-99 will work)."
echo "Example:  \"/Some/Folder/Path/01/01 – Track Name.flac\" becomes \"/Some/Folder/Path/01.01 – Track Name.flac\""

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

# set default file extension


# obtain file extension

echo "By default this script locates and changes "$fileextension" files using case insensivity."
echo "If you would like to change different files (mp3, ape, &c), you will want to change the file extension used in the search."

read -e -p "Please indicate your preferred file extension (leave blank for "$fileextension"): "
   if [ -z "$REPLY" ]; then
      echo "The default remains "$fileextension"."

echo "I will now change all files within numbered (00-99) folders under the directory "$directory" if they have the file extension "$fileextension" to include the folder number followed by a dot (i.e. 01.) and move that file into its parent directory."
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" ) 

      # set case insensitivity
      shopt -s nocaseglob

      # change file names in sub folder

      for filepath in "/$discpath/$disc/"*.$fileextension; do
         filename=$( basename "$filepath" )
         mv "$discpath/$disc/$filename" "$discpath/$disc.$filename"
         echo $filename changed

      # unset case insensitivity
      shopt -u nocaseglob

   done < /tmp/whyme

rm /tmp/whyme



I hope this serves you well. Feel free to send me questions in the comments.


One thought on “Improved Recursive Prepending Script

Leave a Reply

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