Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Last active July 25, 2023 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmarreck/e063c2d7aa854772909a029b9b2f9d4e to your computer and use it in GitHub Desktop.
Save pmarreck/e063c2d7aa854772909a029b9b2f9d4e to your computer and use it in GitHub Desktop.
Convert terrible 8.3-formatted font filenames to their internal fullname using the Fontconfig library tools.
#!/usr/bin/env bash
needs() {
local bin="$1";
shift;
command -v "$bin" > /dev/null 2>&1 || {
printf "%s is required but it's not installed or in PATH; %s\n" "$bin" "$*" 1>&2;
return 1
}
}
needs fc-scan install fontconfig || exit 1
for file in *.{ttf,TTF,otf,OTF,ttc,TTC,pfm,PFM,pfb,PFB}; do
# Extract fullname and take the first item if comma-separated
fontname=$(fc-scan --format "%{fullname}\n" "$file" | awk -F ',' '{print $1}' | tr -dc '[:alnum:][:space:]()_\-.\[\]')
# If the fullname is empty, skip renaming
if [ -z "$fontname" ]; then
echo "Could not extract a name for file: $file. Skipping..." >&2
continue
fi
# Get file extension and rename the file
ext="${file##*.}"
# downcase it. requires bash 4+
ext="${ext,,}"
newname="$fontname.$ext"
# If the new name is the same as the old name, continue
if [ "$file" = "$newname" ]; then
echo "File $file already has the correct name. Skipping..." >&2
continue
fi
# If a file with the new name already exists and they are the same, delete the old file
if [ -e "$newname" ] && cmp --silent "$file" "$newname"; then
rm "$file"
echo "Removed duplicate file: $file"
else
mv "$file" "$newname"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment