This script renames files in the current directory to ensure they have clean, consistent, and cross-platform friendly filenames. It addresses common issues with special characters, extra dots, and unsupported symbols that can cause issues on certain filesystems.
- Removes or replaces problematic characters (like colons
:
, accents, and special symbols). - Converts spaces (
_
) for better cross-platform compatibility. - Preserves file extensions correctly (e.g.,
.mp3
stays intact). - Prevents the script itself (
fix_filenames.sh
) from being renamed. - Ensures filenames contain only letters, numbers, underscores (
_
), and hyphens (-
).
- Download the script and place it in the directory containing the files you want to rename.
- Make the script executable using the following command:
chmod +x fix_filenames.sh
- Run the script in the directory with the files you want to clean up:
./fix_filenames.sh
- The script will rename all files in the current directory and print the changes to the console.
Before Running the Script:
01 J. Strauss II: Overture Die Fledermaus - arr. Winter.mp3
02 Elgar: Variations on an Original Theme, Op.36 "Enigma" - arr. Wright - Nimrod.mp3
17 Dvoř�ák: 8 Slavonic Dances, Op.46 - Arr. D.Wright - 8. No.8 in G minor.mp3
fix_filenames.sh
After Running the Script:
01_J_Strauss_II-_Overture_Die_Fledermaus_-_arr_Winter.mp3
02_Elgar-_Variations_on_an_Original_Theme_Op36_Enigma_-_arr_Wright_-_Nimrod.mp3
17_Dvorak-_8_Slavonic_Dances_Op46_-_Arr_DWright_-_8_No8_in_G_minor.mp3
fix_filenames.sh (not renamed)
-
Skip Renaming the Script
if [ "$file" == "fix_filenames.sh" ]; then continue fi
The script checks if the file is itself (
fix_filenames.sh
) and skips it to avoid accidental renaming. -
Separate File Name and Extension
base="${file%.*}" extension="${file##*.}"
This splits the filename into two parts:
base
: the part of the filename before the last dot.extension
: the part after the last dot (e.g.,mp3
,txt
,jpg
).
-
Clean the Filename
newbase=$(echo "$base" | \ sed 's/[:]/-/g' | \ sed 'y/řáéíóúýčďěňšťů/raeiouycdentsu/' | \ sed 's/ /_/g' | \ sed 's/[^a-zA-Z0-9_-]//g' \ )
This part does several cleanup steps:
- Replace colons (
:
) with dashes (-
). - Convert accented characters (like
ř
tor
,á
toa
, etc.) to plain ASCII characters. - Replace spaces (
_
). - Remove any characters that are not letters, numbers, hyphens (
-
), or underscores (_
).
- Replace colons (
-
Remove Extra Dots from Filename (Not Extension)
newbase=$(echo "$newbase" | sed 's/\.//g')
This removes any unnecessary dots from the base name.
-
Recombine Filename and Extension
newfile="${newbase}.${extension}"
The cleaned filename is then reassembled with its original extension.
-
Rename the File
if [ "$file" != "$newfile" ]; then mv "$file" "$newfile" echo "Renamed: '$file' -> '$newfile'" fi
If the new filename is different from the original, the file is renamed, and the change is printed to the console.
#!/bin/bash
for file in *; do
# Skip renaming this script itself
if [ "$file" == "fix_filenames.sh" ]; then
continue
fi
# Separate the filename from its extension
base="${file%.*}"
extension="${file##*.}"
# Clean up the base name
newbase=$(echo "$base" | \
sed 's/[:]/-/g' | \
sed 'y/řáéíóúýčďěňšťů/raeiouycdentsu/' | \
sed 's/ /_/g' | \
sed 's/[^a-zA-Z0-9_-]//g' \
)
# Remove extra dots from the filename (dots should not be in the base name)
newbase=$(echo "$newbase" | sed 's/\.//g')
# Combine the new base with the original extension
newfile="${newbase}.${extension}"
# Rename the file only if the new name is different
if [ "$file" != "$newfile" ]; then
mv "$file" "$newfile"
echo "Renamed: '$file' -> '$newfile'"
fi
done
- The script assumes that the file extension is correct and does not validate it.
- It works only on files in the current directory.
- Files with identical cleaned filenames may overwrite each other (e.g.,
file1.txt
andfile-1.txt
both becomingfile1.txt
).
This script is open-source and licensed under the MIT License. You are free to use, modify, and distribute it as needed.
If you'd like to improve this script, feel free to open a pull request or issue on the GitHub repository.
If you have any questions or suggestions, feel free to open an issue or submit a pull request. Happy coding! 😎