gpt4 book ai didi

linux - Linux 命令 `file` 如何识别我的文件的编码?

转载 作者:太空宇宙 更新时间:2023-11-04 04:50:55 28 4
gpt4 key购买 nike

Linux 命令 file 如何识别我的文件的编码?

zell@ubuntu:~$ file examples.desktop 
examples.desktop: UTF-8 Unicode text

zell@ubuntu:~$ file /etc/services
/etc/services: ASCII text

最佳答案

man page很清楚

The filesystem tests are based on examining the return from a stat(2) system call...

The magic tests are used to check for files with data in particular fixed formats. The canonical example of this is a binary executable (compiled program) a.out file, whose format is defined in #include and possibly #include in the standard include directory. These files have a 'magic number' stored in a particular place near the beginning of the file that tells the UNIX operating system that the file is a binary executable, and which of several types thereof. The concept of a 'magic' has been applied by extension to data files. Any file with some invariant identifier at a small fixed offset into the file can usually be described in this way. The information identifying these files is read from the compiled magic file /usr/share/misc/magic.mgc, or the files in the directory /usr/share/misc/magic if the compiled file does not exist. In addition, if $HOME/.magic.mgc or $HOME/.magic exists, it will be used in preference to the system magic files. If /etc/magic exists, it will be used together with other magic files.

If a file does not match any of the entries in the magic file, it is examined to see if it seems to be a text file. ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets (such as those used on Macintosh and IBM PC systems), UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC character sets can be distinguished by the different ranges and sequences of bytes that constitute printable text in each set. If a file passes any of these tests, its character set is reported.

简而言之,对于常规文件,其 magic values被测试。如果不匹配,则 file 检查它是否是文本文件,通过查看文件中字节的实际值对特定编码进行有根据的猜测。

哦,你也可以download the source code并亲自查看实现。

关于linux - Linux 命令 `file` 如何识别我的文件的编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58328993/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com