Skip to content

Instantly share code, notes, and snippets.

@jmatthewturner
Last active May 29, 2025 17:58
Show Gist options
  • Save jmatthewturner/68927ab4b9498514f4f91216aeb00c46 to your computer and use it in GitHub Desktop.
Save jmatthewturner/68927ab4b9498514f4f91216aeb00c46 to your computer and use it in GitHub Desktop.
BASH script for batch renaming FLAC/MP3 files with a given prefix
#!/bin/bash
# This script was written entirely by Claude.ai, with prompts by jmatthewturner.
# It does what I need well enough, so I'm not taking the time to refine it.
# But I still wanted to share in case anyone else finds it useful.
#
# It's purpose is to batch rename a group of music files, as part of a larger
# project to get a Kenwood Excelon head unit working properly with USB MP3s.
# Details of the larger project are here:
# https://jmatthewturner.wordpress.com/2025/05/29/usb-mp3-functionality-on-kenwood-excelon-head-units/
#
# https://gist.github.com/jmatthewturner/68927ab4b9498514f4f91216aeb00c46
#
# Everything after this line is AI generated.
# Script to add a prefix to all MP3 and FLAC filenames in the current directory
# Prompt user for the string to use as prefix
echo "Enter the string to add as prefix to all MP3 and FLAC filenames:"
read PREFIX_STRING
# Check if user provided input
if [ -z "$PREFIX_STRING" ]; then
echo "Error: No prefix provided. Exiting."
exit 1
fi
# Counter for renamed files
count=0
# Confirm with the user
echo "This will add '$PREFIX_STRING' as a prefix to all MP3 and FLAC filenames in the current directory."
echo "Are you sure you want to continue? (y/n)"
read confirm
# Check user confirmation
if [[ ! "$confirm" =~ ^[Yy] ]]; then
echo "Operation cancelled."
exit 0
fi
# Iterate through MP3 and FLAC files in the current directory
for file in *.mp3 *.flac; do
# Skip if it's a directory, the script itself, or if no files were found
if [ -d "$file" ] || [ "$file" = "$(basename "$0")" ] || [ "$file" = "*.mp3" ] || [ "$file" = "*.flac" ]; then
continue
fi
# Create the new filename with prefix
new_filename="${PREFIX_STRING}${file}"
# Rename the file
mv "$file" "$new_filename"
# Increment the counter and print feedback
((count++))
echo "Renamed: $file -> $new_filename"
done
echo "Complete! Renamed $count files."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment