Skip to content

Instantly share code, notes, and snippets.

@vijinho
Created January 6, 2025 10:07
Show Gist options
  • Save vijinho/8c8bc86754651e82315beb82057f50db to your computer and use it in GitHub Desktop.
Save vijinho/8c8bc86754651e82315beb82057f50db to your computer and use it in GitHub Desktop.
bash shell script to transliterate a file - that is, to strip any accents from the file and make it standard English alphabetic characters
#!/bin/bash
# Parse command-line options
while getopts ":f:" opt; do
case ${opt} in
f)
file=$OPTARG
;;
\?)
echo "Usage: $0 [-f /path/to/file] or [--file /path/to/file]"
exit 1
;;
:)
echo "Invalid option: -$OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
# Check if the file argument is provided
if [ -z "$file" ]; then
echo "Error: No file specified."
echo "Usage: $0 [-f /path/to/file] or [--file /path/to/file]"
exit 1
fi
# Check if the file exists
if [ ! -f "$file" ]; then
echo "Error: File not found: $file"
exit 1
fi
new_name=$(echo "$file" | iconv -f UTF-8 -t ASCII//TRANSLIT)
# Check if the new name is different from the original name
if [ "$new_name" != "$file" ]; then
mv -- "$file" "$new_name"
echo "File renamed successfully: $file -> $new_name"
else
echo "No change needed. File name already in ASCII: $file"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment