linux +验证文件是文本还是二进制 [英] linux + verify if file is text or binary
问题描述
如何在不打开文件的情况下验证文件是二进制文件还是文本文件?
How can I verify if the file is binary or text without to open the file?
推荐答案
薛定ding的猫.
没有打开文件就无法确定文件内容的方法.文件系统不存储与内容有关的元数据.
There is no way to determine the contents of a file without opening it. The filesystem stores no metadata relating to the contents.
如果不是必须打开文件,那么有许多解决方案可供您选择.
If not opening the file is not a hard requirement, then there are a number of solutions available to you.
在许多评论和答案中都建议file(1)
是确定内容的一种好方法.它的确是.但是,file(1)
打开文件,该文件在问题中被禁止.请参见以下示例中的倒数第二行:
It has been suggested in a number of comments and answers that file(1)
is a good way of determining the contents. Indeed it is. However, file(1)
opens the file, which was prohibited in the question. See the penultimate line in the following example:
> echo 'This is not a pipe' > file.jpg && strace file file.jpg 2>&1 | grep file.jpg
execve("/usr/bin/file", ["file", "file.jpg"], [/* 56 vars */]) = 0
lstat64("file.jpg", {st_mode=S_IFREG|0644, st_size=19, ...}) = 0
stat64("file.jpg", {st_mode=S_IFREG|0644, st_size=19, ...}) = 0
open("file.jpg", O_RDONLY|O_LARGEFILE) = 3
write(1, "file.jpg: ASCII text\n", 21file.jpg: ASCII text
这篇关于linux +验证文件是文本还是二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!