Elegant way to search for UTF-8 files with BOM?

Posted by vog on Stack Overflow See other posts from Stack Overflow or by vog
Published on 2008-10-15T13:48:29Z Indexed on 2010/05/18 15:40 UTC
Read the original article Hit count: 116

Filed under:
|
|
|

For debugging purposes, I need to recursively search a directory for all files which start with a UTF-8 byte order mark (BOM). My current solution is a simple shell script:

find -type f |
while read file
do
    if [ "`head -c 3 -- "$file"`" == $'\xef\xbb\xbf' ]
    then
        echo "found BOM in: $file"
    fi
done

Or, if you prefer short, unreadable one-liners:

find -type f|while read file;do [ "`head -c3 -- "$file"`" == $'\xef\xbb\xbf' ] && echo "found BOM in: $file";done

It doesn't work with filenames that contain a line break, but such files are not to be expected anyway.

Is there any shorter or more elegant solution?

Are there any interesting text editors or macros for text editors?

© Stack Overflow or respective owner

Related posts about utf-8

Related posts about shell-scripting