I want to use a Unix shell command to find all UTF-16 encoded files (containing the UTF-16 Byte Order Mark (BOM)) in a directory tree. Is there a command that I can use?
|
Though you asked to find the BOM, using
Hence, for example: find . -type f -exec file --mime {} \; | grep "charset=utf-16"
| |||
|
feedback
|
|
You can use
(Tested with Explanation: The $(echo... part generates the BOM (Hex FE FF, as octal escape sequences), this is then fed to -r is recursive search, -l makes grep print the names of files it found (instead of the matching line). This might be a bit wasteful, as grep will scan each file completely, rather than just the start. If it's mostly small text files, it will not matter. If you have loads of files with several MB, you'll have to write a perl script :-). Alternatively, you could try | |||||
feedback
|
|
If you have it, you can use
| ||||
|
feedback
|
|
Thanks for the help everybody. What worked best on my Mac was:
It's based on sleske's solution, but takes into account that the Byte Order Mark can be reversed. It also uses awk to stop looking for the BOM after the first line, since the BOM must be at the beginning of the file. The \x escaping used to specify the BOM works with bash, I don't know if it works with other shells. The enca tool suggested by ghostdog74 also will do the job, but it wasn't present on my Mac. | |||
|
feedback
|
|
Here is the script that I use to find UTF-16 files, and subsequently convert them to UTF-8. #!/bin/sh
| |||
|
feedback
|