How to find binaries in a directory?

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

Thanks.

+8
linux file directory grep binary
source share
6 answers

Just worth mentioning is the Perl -T test for text files and its opposite -B for binary files.

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

prints any binary files that it sees. Use -T if you want the opposite: text files.

It is not completely reliable because it only looks in the first thousand characters or so, but it is better than some of the ad-hoc methods suggested here. See Man perlfunc for a complete discussion. Here is a summary:

The switches "-T" and "-B" work as follows. The first block or so the file is checked to see if it is valid UTF-8, which includes non-ASCII characters. If so, then the "-T" file. Otherwise, the same part of the file is considered for odd characters, such as strange control codes or characters with a high set of bits. If more than a third of the characters are strange, this is the -B file; otherwise it is a "-T" file. In addition, any file containing zero bytes in the part under investigation is considered a binary file.

+8
source share

It finds all non-text, binary and empty files.

Edit

Solution only with grep (from Mehrdad comment):

 grep -r -I -L . 

Original answer

No tool other than find and grep is required for this:

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

-I tells grep that binaries should be second to none

-L prints only unmatched files

. matches anything else


Edit 2

This finds all non-empty binaries:

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

Since this is an appointment, you probably hate me if I give you a complete solution ;-) So, here is a small hint:

The grep lists the default binaries if you are looking for a regular expression of the type . Which will match any non-empty file:

 grep . * 

Output:

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

You can use awk to get only file names and ls to print permissions. See the related manual pages ( man grep , man awk , man ls ).

+2
source share

My first answer to this question turned out to be pretty built-in with the find . I think that your instructor tried to introduce you to the concept of magic numbers using the file command, which breaks them into several types.

For my purposes, it was as simple as:

 file * | grep executable 

But this can be done in many ways.

0
source share

Linux binaries are in ELF format

When you run the file command for a binary file, the output contains the word ELF . You can figure it out.

At the command line:

file <binary_file_name>

So, if you want to find binaries in a directory (for example, on Linux), you can do something like this:

ls | xargs file | grep ELF

-one
source share

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

The files say:

  -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 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 the result of what you want:

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

All Articles