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

时间:2022-07-23 15:05:06

I need to find the binary files in a directory. I want to do this with file, and after that i will check the results with grep. But my problem is that I have no idea what is a binary file. What will give the file command for binary files or what should I check with grep?

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

Thank you.

4 个解决方案

#1


5  

Just have to mention Perl's -T test for text files, and its opposite -B for binary files.

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

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

will print out any binary files it sees. Use -T if you want the opposite: text files.

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

It's not totally foolproof as it only looks in the first 1,000 characters or so, but it's better than some of the ad-hoc methods suggested here. See man perlfunc for the whole rundown. Here is a summary:

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

The "-T" and "-B" switches work as follows. The first block or so of the file is examined to see if it is valid UTF-8 that includes non-ASCII characters. If, so it's a "-T" file. Otherwise, that same portion of the file is examined for odd characters such as strange control codes or characters with the high bit set. If more than a third of the characters are strange, it's a "-B" file; otherwise it's a "-T" file. Also, any file containing a zero byte in the examined portion is considered a binary file.

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

#2


4  

I will share this line, which does not require any other tool except find and grep:

我将分享这一行,除了find和grep之外不需要任何其他工具:

find . -type f -exec grep -IL . "{}" \; > ../binary-files

-I tells grep to assume binary files as unmatched, -L prints only unmatched files, . matches anything else.

-I告诉grep假设二进制文件不匹配,-L只打印不匹配的文件。匹配其他任何东西

#3


2  

As this is an assignment, you would probably hate me if I gave you the complete solution ;-) So here is a little hint:

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

The grep command will output a list of binary files per default, if you search for a regular expression like . that will match on any non-empty file:

如果搜索正则表达式,grep命令将默认输出二进制文件列表。将匹配任何非空文件:

grep . *

Output:

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

You can use awk to get the filenames only and ls to print the permissions. See the respective man pages (man grep, man awk, man ls).

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

#4


-3  

You can use find and the parameter -executable that is basically what you want.

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

The manpages says:

这些联机帮助页说:

   -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.

This is a result of what you want:

这是你想要的结果:

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

#1


5  

Just have to mention Perl's -T test for text files, and its opposite -B for binary files.

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

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

will print out any binary files it sees. Use -T if you want the opposite: text files.

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

It's not totally foolproof as it only looks in the first 1,000 characters or so, but it's better than some of the ad-hoc methods suggested here. See man perlfunc for the whole rundown. Here is a summary:

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

The "-T" and "-B" switches work as follows. The first block or so of the file is examined to see if it is valid UTF-8 that includes non-ASCII characters. If, so it's a "-T" file. Otherwise, that same portion of the file is examined for odd characters such as strange control codes or characters with the high bit set. If more than a third of the characters are strange, it's a "-B" file; otherwise it's a "-T" file. Also, any file containing a zero byte in the examined portion is considered a binary file.

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

#2


4  

I will share this line, which does not require any other tool except find and grep:

我将分享这一行,除了find和grep之外不需要任何其他工具:

find . -type f -exec grep -IL . "{}" \; > ../binary-files

-I tells grep to assume binary files as unmatched, -L prints only unmatched files, . matches anything else.

-I告诉grep假设二进制文件不匹配,-L只打印不匹配的文件。匹配其他任何东西

#3


2  

As this is an assignment, you would probably hate me if I gave you the complete solution ;-) So here is a little hint:

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

The grep command will output a list of binary files per default, if you search for a regular expression like . that will match on any non-empty file:

如果搜索正则表达式,grep命令将默认输出二进制文件列表。将匹配任何非空文件:

grep . *

Output:

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

You can use awk to get the filenames only and ls to print the permissions. See the respective man pages (man grep, man awk, man ls).

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

#4


-3  

You can use find and the parameter -executable that is basically what you want.

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

The manpages says:

这些联机帮助页说:

   -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.

This is a result of what you want:

这是你想要的结果:

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