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

问题描述 投票:5回答:6

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

谢谢。

linux file grep binary directory
6个回答
7
投票

只需要提到Perl的-T测试文本文件,以及它的二进制文件的-B

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

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

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

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


5
投票

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

Edit

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

grep

Original answer

这不需要除grep -r -I -L . find之外的任何其他工具:

grep

find . -type f -exec grep -IL . "{}" \; 告诉grep假设二进制文件不匹配

-I只打印不匹配的文件

-L匹配其他任何东西


Edit 2

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

.

2
投票

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

find . -type f ! -size 0 -exec grep -IL . "{}" \; 命令将默认输出二进制文件列表,如果您搜索grep之类的正则表达式,它将匹配任何非空文件:

.

输出:

grep . *

您可以使用[...] Binary file c matches Binary file e matches 来获取文件名,使用awk来打印权限。请参见相应的手册页(lsman grepman awk)。


0
投票

这个问题的第一个答案在这里使用man ls命令。我认为你的导师希望使用find命令让你进入magic numbers的概念,将它们分解为多种类型。

就我的目的而言,它很简单:

file

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


-1
投票

linux中的二进制文件格式为file * | grep executable

在二进制文件上运行ELF命令时,输出包含单词file。你可以这样做。

在命令行上:

ELF

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

file <binary_file_name>


-3
投票

您可以使用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.
© www.soinside.com 2019 - 2024. All rights reserved.