Skip to content

Instantly share code, notes, and snippets.

@gentam
Created June 17, 2018 08:41
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 gentam/5758f8a46b48964aabceffdc11c45600 to your computer and use it in GitHub Desktop.
Save gentam/5758f8a46b48964aabceffdc11c45600 to your computer and use it in GitHub Desktop.
convert a text file or all files under a directory to UTF-8 using nkf
#!/bin/sh
opt='-w'
# lower case -> output / UPPER CASE -> input
# -J ISO-2022-JP (JIS code)
# -S Shift_JIS
# -E EUC-JP
# -W UTF-8
[ $# -eq 0 ] && echo "Usage: `basename $0` <filename | dirname>" \
&& exit 1
self="$0"
if [ -f "$@" ]; then
# [ $(nkf -g "$@") = 'Shift_JIS' ] && \ # only convert specific encoding
nkf --in-place $opt "$@"
echo "converted $@"
elif [ -d "$@" ]; then
# given directory, convert all files recursively
find "$@" -type f -print0 | xargs -0 -n1 $self
else
echo "$@: No such file or directory"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment