Skip to content

Instantly share code, notes, and snippets.

@ecormaksin
Created June 8, 2023 01:04
Show Gist options
  • Save ecormaksin/d693a574a1b1ccc33ce78b2339d922e1 to your computer and use it in GitHub Desktop.
Save ecormaksin/d693a574a1b1ccc33ce78b2339d922e1 to your computer and use it in GitHub Desktop.
change file encoding to utf8-lf
#!/bin/bash
if [ $# -gt 2 -o $# -lt 1 ]; then
echo "Usage: $0 TARGET_FOLDER_PATH [TARGET_FILE_REGEX]"
exit 1
fi
TARGET_FOLDER_PATH=$1
if [ $# -eq 1 ]; then
TARGET_FILE_REGEX = ".*"
else
TARGET_FILE_REGEX=$2
fi
find "${TARGET_FOLDER_PATH%/}/" -type f -regex $TARGET_FILE_REGEX 2>/dev/null | while read -r A_FILE_PATH
do
if [[ "${A_FILE_PATH}" =~ "/.venv/" ]]; then
continue
fi
FILE_ENCODING=`nkf --guess "${A_FILE_PATH}"`
if [[ "${FILE_ENCODING}" =~ "ASCII" ]]; then
continue
fi
if [ "${FILE_ENCODING}" = "UTF-8 (LF)" ]; then
continue
fi
echo "${A_FILE_PATH}: ${FILE_ENCODING}"
nkf -wd --overwrite "${A_FILE_PATH}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment