#!/bin/bash
find . -type f \( -name "*.java" -o -name "*.xml" -o -name "*.properties" \) | while read file; do
# BOMの有無を先頭3バイトで判定
bom=$(xxd -p -l 3 "$file")
if [ "$bom" = "efbbbf" ]; then
echo "変換中(BOMあり → なし + LF): $file"
# BOM除去してUTF-8として再保存
tail --bytes=+4 "$file" | dos2unix > "$file.tmp" && mv "$file.tmp" "$file"
else
echo "変換中(その他 → UTF-8 + LF): $file"
encoding=$(file -i "$file" | cut -d '=' -f 2)
if [ "$encoding" != "utf-8" ]; then
iconv -f "$encoding" -t UTF-8 "$file" > "$file.tmp" && mv "$file.tmp" "$file"
fi
dos2unix "$file" > /dev/null 2>&1
fi
done
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme