0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

文字コード改行コード混在のSpringBootプロジェクトをUTF-8+LFに統一する

Posted at
#!/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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?