UTF-8 BOM を確認・追加・削除するコマンド [Linux, macOS 編]

Linux や macOS にて UTF-8 BOM を削除するコマンドをご紹介します。

Linux

UTF-8 BOM 有無の確認

UTF-8 BOM の有無は file コマンドで確認できます。

$ file sample.txt
sample.txt: ASCII text # UTF8 without BOM
$ file sample.txt
sample.txt: UTF-8 Unicode (with BOM) text # UTF8 without BOM

UTF-8 BOM の追加・削除

UTF-8 BOM は nkf コマンドで追加や削除できます。

$ nkf --overwrite --oc=UTF-8 sample.txt # Add BOM
sample.txt: ASCII text
$ nkf --overwrite --oc=UTF-8-BOM sample.txt # Remove BOM
sample.txt: UTF-8 Unicode (with BOM) text

以上、UTF-8 BOM を Linux や macOS の Terminal から削除したい、現場からお送りしました。