如何查找可执行文件类型?

Joh*_*mBF 7 bash find executable elf files

我想找到从内核的角度来看可执行的文件类型。据我所知,Linux 上的所有可执行文件都是 ELF 文件。因此,我尝试了以下操作:

find * | file | grep ELF

但是这不起作用;有人有其他想法吗?

fro*_*boo 12

稍后编辑:只有这个可以满足 jan 的需要:谢谢惠更斯;

find . -exec file {} \; | grep -i elf

  • 我认为你的第一个命令不起作用。它正在搜索名称如“*elf *”的文件(不考虑大小写)。您想要 grep 输出,就像在第二个提案中所做的那样。 (2认同)
  • 这将找到文件名中包含“elf”的文件。我发现对“elf”的 grep 对于我的应用程序来说已经足够好了,但在最常见的情况下,名为“this is not elf .txt”的文件会给出误报。 (2认同)

Ale*_*Che 7

对于有限(例如嵌入式)系统上的系统,不使用file和 的替代解决方案:readelf

find $WHERE -type f -exec hexdump -n 4 -e '4/1 "%2x" " {}\n"'  {} \; | grep ^7f454c46
Run Code Online (Sandbox Code Playgroud)

基本上,我们输出前四个字节并hexdump使用它们作为签名。然后我们可以使用其签名来grep 所有ELF类型的文件7f454c46

或者,由于7f删除字符45, 4c,46字节分别是E, L,F字符,我们也可以使用:

find $WHERE -type f -exec hexdump -n 4 -e '4/1 "%1_u" " {}\n"'  {} \; | grep ^delELF
Run Code Online (Sandbox Code Playgroud)

head另外,在这种情况下您可以使用hexdump

find $WHERE -type f -exec head -c 4 {} \; -exec echo " {}" \;  | grep ^.ELF
Run Code Online (Sandbox Code Playgroud)