当一个文件只包含一个空格、换行符或 ASCII 字母时,为什么文件命令说它“没有魔法”?
用两个相同的字符破坏文件,
然后文件将其称为 ASCII.
一个非常小的文件 with magic 会是什么? 为什么要创建它以及它的用途是什么?
> /bin/rm junk
> printf ' ' > junk; file junk
junk: very short file (no magic)
> echo > junk; file junk
junk: very short file (no magic)
> printf 'd' > junk; file junk
junk: very short file (no magic)
> printf 'dd' > junk; file junk
junk: ASCII text, with no line terminators
以下帖子似乎相关;它因缺乏细节或清晰度而关闭:
https://unix.stackexchange.com/questions/393288/explain-please-what-is-a-magic-file-in-unix
但我不是在问魔法是什么或如何使文件变魔术。 我问为什么
file
对一个字节的文件说“没有魔法”,但对两个字节的文件却不是。
为什么文件对一个字节的文件说“没有魔法”,但对两个字节的文件却不是。
因为
file
程序就是为了做到这一点而编写的。
来自https://github.com/file/file/blob/c49972ddb4933ee6edc4a2abc7f36f6a42d47300/src/funcs.c#L338:
if (nb == 0) {
def = "empty";
goto simple;
} else if (nb == 1) {
def = "very short file (no magic)";
goto simple;
}