繁体   English   中英

如何在目录中查找二进制文件?

[英]How to find binary files in a directory?

我需要在目录中找到二进制文件。 我想用文件来做这个,然后我会用 grep 检查结果。 但我的问题是我不知道什么是二进制文件。 什么会给二进制文件的文件命令,或者我应该用 grep 检查什么?

这将查找所有非基于文本的二进制文件和空文件。

编辑

只有grep解决方案(来自 Mehrdad 的评论):

grep -rIL .

原答案

除了findgrep这不需要任何其他工具:

find . -type f -exec grep -IL . "{}" \;

-I告诉 grep 假设二进制文件不匹配

-L只打印不匹配的文件

. 匹配其他任何东西


编辑 2

这将查找所有非空二进制文件:

find . -type f ! -size 0 -exec grep -IL . "{}" \;

只需要提及Perl对文本文件的-T测试,以及对二进制文件的相反-B测试。

$ find . -type f | perl -lne 'print if -B'

将打印出它看到的任何二进制文件。 如果您想要相反的内容,请使用-T :文本文件。

它并非完全万无一失,因为它只显示前 1,000 个字符左右,但它比此处建议的一些特别方法要好。 有关整个纲要,请参阅man perlfunc 这是一个总结:

“-T”和“-B”开关的工作方式如下。 检查文件的第一个块左右,看它是否是包含非 ASCII 字符的有效 UTF-8。 如果,那么它是一个“-T”文件。 否则,将检查文件的同一部分是否有奇数字符,例如奇怪的控制代码或设置了高位的字符。 如果超过三分之一的字符是奇怪的,那就是“-B”文件; 否则它是一个“-T”文件。 此外,任何在检查部分包含零字节的文件都被视为二进制文件。

我对这个问题的第一个答案在这里使用find命令几乎是内联的。 我认为您的讲师希望使用file命令让您了解magic numbers的概念,该命令将它们分解为多种类型。

就我而言,它很简单:

file * | grep executable

但它可以通过多种方式完成。

由于这是一项任务,如果我给你完整的解决方案,你可能会讨厌我;-) 所以这里有一点提示:

grep命令默认会输出一个二进制文件列表,如果你搜索像. 这将匹配任何非空文件:

grep . *

输出:

[...]
Binary file c matches
Binary file e matches

您可以使用awk仅获取文件名,使用ls打印权限。 请参阅相应的手册页( man grepman awkman ls )。

在这些现代(毕竟2020 年实际上是 21 世纪的第 3 个十年),我认为正确的问题是如何找到所有非 utf-8 文件 UTF-8 是现代文本文件的等价物。

具有非 ascii 代码点的文本的 utf-8 编码将引入非 ascii 字节(即设置了最高有效位的字节)。 现在,并非所有此类字节的序列都形成有效的 utf-8 序列。

moreutils包中的isutf8正是您所需要的。

$ isutf8 -l /bin/*
/bin/[
/bin/acyclic
/bin/addr2line
/bin/animate
/bin/applydeltarpm
/bin/apropos
⋮

快速检查:

$ file $(isutf8 -l /bin/*)
/bin/[:             ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=4d70c2142fc672d8a69d033ecb6693ec15b1e6fb, for GNU/Linux 3.2.0, stripped
/bin/acyclic:       ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=d428ea52eb0e8aaf7faf30914710d8fbabe6ca28, for GNU/Linux 3.2.0, stripped
/bin/addr2line:     ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=797f42bc4f8fb754a49b816b82d6b40804626567, for GNU/Linux 3.2.0, stripped
/bin/animate:       ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=36ab46e69c1bfea433382ffc9bbd9708365dac2b, for GNU/Linux 3.2.0, stripped
/bin/applydeltarpm: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=a1fddcbeec9266e698782596f2dfd1b4f3e0b974, for GNU/Linux 3.2.0, stripped
/bin/apropos:       symbolic link to whatis
⋮

您可能希望反转测试并获取所有文本文件。 使用-i

$ isutf8 -il /bin/*
/bin/alias
/bin/bashbug
/bin/bashbug-64
/bin/bg
⋮
$ file -L $(isutf8 -il /bin/*)
/bin/alias:      a /usr/bin/sh script, ASCII text executable
/bin/bashbug:    a /usr/bin/sh - script, ASCII text executable, with very long lines
/bin/bashbug-64: a /usr/bin/sh - script, ASCII text executable, with very long lines
/bin/bg:         a /usr/bin/sh script, ASCII text executable
⋮

是的,它读取整个文件,但速度非常快,如果你想要准确性......

我需要在目录中找到二进制文件。 我想使用文件执行此操作,然后我将使用grep检查结果。 但是我的问题是我不知道什么是二进制文件。 什么将给二进制文件file命令,或者我应该使用grep检查什么?

我认为确定文件性质的最佳工具是文件实用程序。 在我的一个目录中,我只有一个文件被 nautilus 文件管理器识别为二进制文件。 仅对于此文件,命令ls | xargs 文件返回“数据”而没有任何进一步的信息。

linux中二进制文件的格式为ELF

当您对二进制文件运行file命令时,输出包含单词ELF 你可以grep这个。

在命令行上:

file <binary_file_name>

所以,如果你想在一个目录中找到二进制文件(例如在 linux 中),你可以这样做:

ls | xargs file | grep ELF

您可以使用find和参数-executable这基本上就是您想要的。

手册页说:

   -executable
          Matches files which are executable and directories which are searchable (in a file name resolution sense).  This takes into  account  access control lists and other permissions artefacts which the -perm test ignores.  This test makes use of the access(2) system call, and so can be fooled by NFS servers which do UID mapping (or root-squashing), since many systems implement access(2) in the client's kernel and so  cannot make  use  of  the  UID mapping information held on the server.  Because this test is based only on the result of the access(2) system call, there is no guarantee that a file for which this test succeeds can actually be executed.

这是你想要的结果:

# find /bin  -executable -type f | grep 'dmesg'
/bin/dmesg

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM