Skip to content

Instantly share code, notes, and snippets.

@rodrigodev
Created December 8, 2020 21:26
Show Gist options
  • Save rodrigodev/e4dc6d5c654969a7e6492a97603e503b to your computer and use it in GitHub Desktop.
Save rodrigodev/e4dc6d5c654969a7e6492a97603e503b to your computer and use it in GitHub Desktop.
Convert encoding batch for Mac OS
#!/bin/bash
#enter input encoding here
FROM_ENCODING="ISO_8859-1"
#output encoding(UTF-8)
TO_ENCODING="UTF-8"
#convert
CONVERT="iconv -f $FROM_ENCODING -t $TO_ENCODING"
#loop to convert multiple files
for file in ./*.txt; do
encoding=$(file -I "$file" | awk '{print $3}')
if [ "$encoding" == "charset=iso-8859-1" ]; then
$CONVERT "$file" >"$file.new" &&
mv -f "$file.new" "$file"
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment