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

Kis*_*yas 8 linux directory binary grep file

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

谢谢.

bob*_*ogo 8

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

$ find . -type f | perl -lne 'print if -B'
Run Code Online (Sandbox Code Playgroud)

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

它并非完全万无一失,因为它只能看到前1000个字符左右,但它比这里建议的一些特殊方法更好.请参阅man perlfunc了解整个破坏.以下是摘要:

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


t.a*_*mal 6

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

编辑

仅解决方案grep(根据Mehrdad的评论):

grep -r -I -L .
Run Code Online (Sandbox Code Playgroud)

原始答案

find和以外,不需要任何其他工具grep

find . -type f -exec grep -IL . "{}" \;
Run Code Online (Sandbox Code Playgroud)

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

-L 仅打印不匹配的文件

. 匹配其他任何东西


编辑2

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

find . -type f ! -size 0 -exec grep -IL . "{}" \;
Run Code Online (Sandbox Code Playgroud)

  • 我认为你可以将其简化为“grep -r -I -L .”? (3认同)
  • 这在 OSX 中不起作用,因为它会将 html 文件作为二进制文件输出 (2认同)

Bre*_*tão -4

您可以使用基本上您想要的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.
Run Code Online (Sandbox Code Playgroud)

这是您想要的结果:

# find /bin  -executable -type f | grep 'dmesg'
/bin/dmesg
Run Code Online (Sandbox Code Playgroud)

  • `find` 的 `-executable` 选项与权限有关,而不是文件内容。OP 明确提到“二进制”,而不是“可执行”文件。 (9认同)