How can I check if a UTF-8 text file has a BOM from command line?
file command shows me:
UTF-8 Unicode text
But, I don't know if it means there is no BOM in the file.
I'm using Ubuntu 12.04.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
file will tell you if there is a BOM. You can simply test it with:
printf '\ufeff...\n' | file - /dev/stdin: UTF-8 Unicode (with BOM) text
Some shells such as
dash have a
printf builtin that does not support
\u, in which case you need to use
printf from the GNU coreutils, e.g.
Note: according to the
file changelog, this feature existed already in 2007. So, this should work on any current machine.
If you execute
stat fileName it should give you exact the three characters. When I opened the file in the editor, I was unable to see anything. So noticing that the file size was 3 gave me clarity that it has a BOM.
Also, the post here was helful in my case.
hexdump -n 3 -C 2.txt 00000000 ef bb bf ef bb bf // YES