Skip to content

Instantly share code, notes, and snippets.

@yzorg
Last active January 27, 2023 18:35
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 yzorg/e4e9813fc07bef4af10ae43febca38d9 to your computer and use it in GitHub Desktop.
Save yzorg/e4e9813fc07bef4af10ae43febca38d9 to your computer and use it in GitHub Desktop.
find all UTF-16 files and convert to UTF-8
#!/bin/bash
# see https://gist.github.com/yzorg/e4e9813fc07bef4af10ae43febca38d9
# function to convert file to UTF-8
convert_to_utf8() {
iconv -f UTF-16 -t UTF-8 "$1" -o "$1".utf8
mv "$1".utf8 "$1"
}
# output the list before making the 1st change
find . -type f -exec file {} \; | grep "UTF-16"
# find all files in subdirectories and convert them to UTF-8 if they are in Unicode-16 format
find . -type f -exec file {} \; | grep "UTF-16" | awk '{print $1}' |
while read file; do
convert_to_utf8 "${file:0:-1}"
done
@yzorg
Copy link
Author

yzorg commented Jan 25, 2023

initial commit is 100% ChatGPT

@yzorg
Copy link
Author

yzorg commented Jan 27, 2023

  • the ai generated version didn't handle ":" at the end of $1 file output, so changed from $file to ${file:0:-1}
  • it also doesn't handle spaces in directory names (known issue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment