如何在目录(linux)中find二进制文件?

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

谢谢。

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

如果您搜索正则表达式, grep命令将输出默认情况下的二进制文件列表. 这将在任何非空文件上匹配:

 grep . * 

输出:

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

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

不得不提到Perl的文本文件-T测试,其二进制文件与-B相反。

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

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

这并不是完全万无一失,因为它只能看到前1000个字符,但比这里提出的一些特殊方法更好。 看到人perlfunc为整个破败。 这里是一个总结:

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

你可以使用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