Linux file 命令对文件进行分类

use*_*622 18 file-command file-format

我需要识别随机文件中包含的数据类型。我是 Linux 新手。

我打算使用该file命令来了解文件具有的数据类型。我尝试了该命令并得到了下面的输出。

有人向我建议该file命令查看文件的初始字节以确定数据类型。该file命令根本不查看文件扩展名。那是对的吗?我查看了手册页,但觉得它太技术性了。如果有人可以提供一个链接,该链接对file命令的工作方式有更简单的解释,我将不胜感激。

运行file命令后我可以获得哪些不同的可能答案?例如,在下面的成绩单中,我得到了 JPEG、ISO 媒体、ASCII 等:

屏幕输出如下

 m7% file date-file.csv
date-file.csv: ASCII text, with CRLF line terminators
m7% file image-file.JPG
image-file.JPG: JPEG image data, EXIF standard
m7% file music-file.m4a
music-file.m4a: ISO Media, MPEG v4 system, iTunes AAC-LC
m7% file numbers-file.txt
numbers-file.txt: ASCII text
m7% file pdf-file.pdf
pdf-file.pdf: PDF document, version 1.4
m7% file text-file.txt
text-file.txt: ASCII text
m7% file video-file.MOV
video-file.MOV: data
Run Code Online (Sandbox Code Playgroud)


更新 1

感谢您的回答,他们为我澄清了一些事情。

因此,如果我理解正确,文件夹 /usr/share/mime/magic 有一个数据库,它将为我提供当前可能的文件格式(当我键入文件命令并跟随文件时可以获得的输出)。那是对的吗?每当“文件”命令输出包含“文本”一词时,它是指您可以使用文本查看器阅读的内容,而没有“文本”的任何内容都是某种二进制文件,这是真的吗?

Mic*_*mer 14

file 使用几种测试

1:如果文件不存在、无法读取或无法确定其文件状态,则输出应指示该文件已被处理,但无法确定其类型。

这将像cannot open file: No such file or directory.

2:如果该文件不是普通文件,则应标识其文件类型。文件类型目录、FIFO、套接字、块特殊和字符特殊应被识别。也可以识别其他实现定义的文件类型。如果 file 是符号链接,默认情况下应该解析链接并且 file 应该测试符号链接引用的文件类型。(请参阅下面的-h-i选项。)

这将像.: directory和那样输出/dev/sda: block special。这个和上一点的大部分格式是由 POSIX 部分定义的- 您可以依赖输出中的某些字符串。

3:如果文件长度为零,则标识为空文件。

这是foo: empty

4:文件实用程序应检查文件的初始段,并根据位置敏感测试推测其内容。(不保证答案是正确的;请参阅下面的 -d、-M 和 -m 选项。)

5:文件实用程序应检查文件并根据上下文敏感的默认系统测试猜测其内容。(不保证答案正确。)

这两个使用幻数识别,是命令中最有趣的部分。一个神奇的数字是一个字节一个特殊的序列是在一个已知的地方在一个文件中标识其类型。传统上,该位置是前两个字节,但该术语已进一步扩展以包括更长的字符串和其他位置。有关命令中幻数的更多详细信息,请参阅此其他问题file

file命令具有这些数字及其对应的类型的数据库;该数据库通常位于/usr/share/mime/magic,并将文件内容映射到MIME 类型。那里的输出(通常file -i是默认情况下没有得到它的一部分)将是定义的媒体类型或扩展名。“上下文敏感测试”使用相同的方法,但有点模糊。这些都不能保证是正确的,但它们旨在成为很好的猜测。

file也有一个将这些类型映射到名称的数据库,通过它它可以知道它所标识的文件application/pdf可以描述为PDF document. 这些人类可读的名称也可能被本地化为另一种语言。这些将始终是以人而不是机器理解的方式对文件类型的一些高级描述。

您可以获得的大多数不同输出将来自这些阶段。您可以查看magic文件以获取受支持类型的列表以及如何识别它们 - 我的系统知道 376 种不同的类型。给定的名称和支持的类型由您的系统包装和配置决定,因此您的系统可能比我的系统支持更多或更少,但通常有很多。libmagic还包括额外的硬编码测试。

6:文件应标识为数据文件。

这是foo: data,当它根本无法找出有关该文件的任何信息时。

还有其他一些小标签可以出现。可执行 ( +x) 文件将executable在输出中包含“ ”,通常以逗号分隔。该file实现还可以了解一些文件格式的额外的东西要能描述附加分他们,因为在你的“ PDF document, version 1.4”。


Gil*_*il' 9

手册页通常是简洁的参考资料,而不是介绍。从维基百科页面开始。

file只看文件内容,不看文件名。(它还查看一些文件元数据,例如文件类型:目录、符号链接、命名管道等。但在您感兴趣的情况下,重要的是内容。)

file typically guesses the format of a file by looking at the first few bytes and comparing them with a built-in table of magic numbers. For example, if the file begins with %PDF, then file reports “PDF document” (and goes digging further to report the minimum version). For file types that don't start with magic numbers, it contains heuristics, e.g. report “ASCII text” if the first few bytes are all in the printable ASCII range.

The output of file is fragile: it can vary from unix variant to unix variant and from version to version. On Linux, Cygwin and *BSD, the file command supports an option -i which produces predictable output in the form of a MIME media type (IANA manages the list of standard media types). There's aren't as many details and the output is less human-friendly but the output is predictable and computer-friendly.

$ file -i somefile.csv
somefile.csv: text/plain; charset=us-ascii
$ file -i somefile.jpg
somefile.jpg: image/jpeg; charset=binary
$ file -i somefile.pdf
somefile.pdf: application/pdf; charset=binary
Run Code Online (Sandbox Code Playgroud)

Use file --mime-type if you only want the MIME type itself without encoding information, e.g. application/pdf. Pass the option -b if you don't want to display the file name at the beginning of the line.


Ram*_*esh 5

I would like you to read the answer from here. Some of the excerpts from the answer are,

From man page of file command,

file command actually performs 3 tests on determining the file type.

First test

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

Second test

The magic number tests are used to check for files with data in particular fixed formats.

Third test

The language tests look for particular strings (cf names.h) that can appear anywhere in the first few blocks of a file. For example, the keyword .br indicates that the file is most likely a troff(1) input file, just as the keyword struct indicates a C program.

The output of the file command is generally based on the result of any of the tests that succeeds.

Now, assuming the C++ program starts like this, and the third test succeeds,

#include <iostream>
bla
bla
Run Code Online (Sandbox Code Playgroud)

As per the third test the keyword #include particularly specifies it is of type C program though we have a CPP program in hand. Now, when I check,

$ file example.cpp

example.cpp: ASCII C program text
Run Code Online (Sandbox Code Playgroud)

Now, the concepts of object oriented are specific to C++. Let us create a file specific to C++.

I start my C++ program as,

$ file example.cpp

example.cpp: ASCII C program text
Run Code Online (Sandbox Code Playgroud)

Now, when I issue

$ file example.cpp
Run Code Online (Sandbox Code Playgroud)

The output is,

example.cpp: ASCII C++ program text
Run Code Online (Sandbox Code Playgroud)

This basically explains on how file command works on similar files (In this example, C program and C ++ program are treated alike unless and until we use the object oriented features specific to C++).