Skip to content

Instantly share code, notes, and snippets.

@masakk1
Created April 12, 2026 13:56
Show Gist options
  • Select an option

  • Save masakk1/d719e4a4af312b5278f9937f4d0127e0 to your computer and use it in GitHub Desktop.

Select an option

Save masakk1/d719e4a4af312b5278f9937f4d0127e0 to your computer and use it in GitHub Desktop.
Replaces the extension of the songs found in playlists to the same one. It will ONLY look for .m3u files.
#!/bin/bash
# --- WHAT IT DOES ---
# Replaces the extension of the songs found in playlists to the same one.
# It will ONLY look for .m3u files.
# --- WHY ---
# Because I use my uncompressed library on the computer, and a compressed one
# on portable storage or devices.
# --- HOW TO USE ---
# Pass an argument like MyPlaylistFolder/ and edit the song name
# Then save the changes to a file with a suffix
# --- ARGUMENTS ---
# What to replace inside the file
FileExtensionReplace='s/.(flac|mp3|ogg|m4a)/\.m4a/';
FileSuffix='CompressedVersion_';
# --- COLORS ---
NoColor='\033[0m' # Default color
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
# Checking & finding the dir
if [[ $# -ne 1 ]]; then
echo -e "${Yellow}INFO: You can add a directory as an argument!";
echo -e "INFO: Using '.' instead.${NoColor}";
dir="./";
elif [[ -d $1 ]]; then
if [[ $1 =~ /$ ]]; then
dir="$1";
else
dir="$1/";
fi;
else
echo "$1 is NOT a directory.";
exit 1;
fi;
# Show the files to be changed
echo "Reading $dir";
echo "Editing these files:";
for file in $dir*.m3u;
do
filename=$(basename $file);
if ! [[ $filename =~ $FileSuffix ]]; then
echo -e "$filename => ${Green}$FileSuffix$filename${NoColor}";
fi;
done
# Ask for confirmation
echo "A new file with the suffix '$FileSuffix' will be created in the target directory.";
read -p "Are you sure you want to continue? (s/N): " answer;
answer="${answer,,}";
if [[ $answer == 's' ]]; then
for file in $dir*.m3u;
do
filename=$(basename $file);
if ! [[ $filename =~ $FileSuffix ]]; then
echo "Creating $dir$FileSuffix$filename ...";
cat $file \
| sed -E $FileExtensionReplace \
> "$dir$FileSuffix$filename";
fi;
done
fi;
# Finish the program.
# In case it is ran without the terminal.
echo -e -n "\n\n${Red}Program finished. Press ENTER to exit.${NoColor} ";
read;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment