Linux find binary file

How to find binary files in a directory?

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?

What kind of «binary» files are you talking about here? Do you have an appropriate «binary» file on your system anywhere? What does file say about it?

I don’t know what kind of binary files, because my homework doesn’t define it, only: write a shell script using grep command (and others) to find the binary files in a directory and write their permissions. So i don;t know nothing about binary files type.

That seems under-specific to me. I’d ask for clarification. Though given the suggestion to use grep I’m going to guess it means «contains a NUL byte».

All files are binary. «Binary» means you don’t know the actual format of the file or it is not important in the context. Some files are text files. A text file is one where the entire file can be decoded into a text string with a specific character encoding. All files can be decoded using several different character encodings. It is only valid to do so if you know the file is text and use the character encoding that was used to write it.

10 Answers 10

This finds all non-text based, binary, and empty files.

Edit

Solution with only grep (from Mehrdad’s comment):

Original answer

This does not require any other tool except find and grep :

find . -type f -exec grep -IL . "<>" \; 

-I tells grep to assume binary files as unmatched

-L prints only unmatched files

Edit 2

This finds all non-empty binary files:

find . -type f ! -size 0 -exec grep -IL . "<>" \; 

It looks like you’re right. However it’s quite some time ago that I looked into this so I don’t remember why I put the find there. Without the additional fork this it’s also way faster!

Maybe the files you think are ‘non-binary’ are empty? Those show up, too (as they are not text, I guess)

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

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

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:

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.

In these modern times (2020 is practically the 3rd decade of the 21st century after all), I think the correct question is how do I find all the non-utf-8 files? Utf-8 being the modern equivalent of a text file.

Читайте также:  Linux sudo user list

utf-8 encoding of text with non-ascii code points will introduce non-ascii bytes (i.e., bytes with the most significant bit set). Now, not all sequences of such bytes form valid utf-8 sequences.

isutf8 from the moreutils package is what you need.

$ isutf8 -l /bin/* /bin/[ /bin/acyclic /bin/addr2line /bin/animate /bin/applydeltarpm /bin/apropos ⋮ 
$ file $(isutf8 -l /bin/*) /bin/[: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=4d70c2142fc672d8a69d033ecb6693ec15b1e6fb, for GNU/Linux 3.2.0, stripped /bin/acyclic: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=d428ea52eb0e8aaf7faf30914710d8fbabe6ca28, for GNU/Linux 3.2.0, stripped /bin/addr2line: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=797f42bc4f8fb754a49b816b82d6b40804626567, for GNU/Linux 3.2.0, stripped /bin/animate: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=36ab46e69c1bfea433382ffc9bbd9708365dac2b, for GNU/Linux 3.2.0, stripped /bin/applydeltarpm: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=a1fddcbeec9266e698782596f2dfd1b4f3e0b974, for GNU/Linux 3.2.0, stripped /bin/apropos: symbolic link to whatis ⋮ 

You may wish to invert the test and get all the text files. Use -i :

$ isutf8 -il /bin/* /bin/alias /bin/bashbug /bin/bashbug-64 /bin/bg ⋮ $ file -L $(isutf8 -il /bin/*) /bin/alias: a /usr/bin/sh script, ASCII text executable /bin/bashbug: a /usr/bin/sh - script, ASCII text executable, with very long lines /bin/bashbug-64: a /usr/bin/sh - script, ASCII text executable, with very long lines /bin/bg: a /usr/bin/sh script, ASCII text executable ⋮ 

Yeah, it reads the whole file, but it’s pretty speedy, and if you want accuracy…

Источник

How to find binary files in a directory in Linux?

There are several ways to find binary files in a directory on Linux. Below are three methods:

Method 1: Using the «file» command

  • Step 1 — Open a terminal on your Linux machine.
  • Step 2 — Navigate to the directory where you want to search for binary files using the «cd» command

For example, if you want to search for binary files in the «Downloads» directory, you would run the command «cd Downloads».

  • Step 3 — Once you are in the appropriate directory, you can use the «file» command followed by the «*» wildcard character to search for binary files

The command would be «file *». This command will search for binary files in the current directory and will display the name of the file along with the file type.

Method 2: Using the «find» command

  • Step 1 — Open a terminal on your Linux machine.
  • Step 2 — Navigate to the directory where you want to search for binary files using the «cd» command.
  • Step 3 — Run the command «find -type f -exec file <> ; | grep -i ‘binary'»

This command will search for all files in the current directory, and for each file it will run the «file» command to determine the file type and display the name of the file along with the file type.

Читайте также:  Linux загрузить на dvd

Method 3: Using the «find» command with «grep»

  • Step 1 — Open a terminal on your Linux machine.
  • Step 2 — Navigate to the directory where you want to search for binary files using the «cd» command.
  • Step 3 — Run the command «find -type f -print0 | xargs -0 file | grep -i ‘binary'»

This command uses the find command to search for all files in the current directory, uses the xargs command to apply the file command to each file, and then uses the grep command to filter out only the lines that contain the word «binary».

In all the above methods, the «file» command is used to determine the type of file. The output of this command is a string that describes the file type. For example, a file containing binary data will have a string «data» or «executable» in the output of the «file» command.

In method 1, the «*» wildcard is used to search for all files in the current directory, whereas in method 2 and 3 the find command is used to search for files in the current directory. The «-type f» option is used with the find command to search for files only and not directories.

In method 2, the «-exec» option is used with the find command to execute the file command on each file. In method 3, the «xargs» command is used to apply the file command to each file.

In method 1,2 and 3 the grep command is used to filter out only the lines that contain the word «binary» from the output of the file command.

It’s important to note that these commands will search for binary files in the current directory and all subdirectories. If you want to search for binary files in a specific directory, you can specify the path to that directory with the «find» command (example: find /path/to/directory -type f -exec file <> ; | grep -i ‘binary’)

Another method to find binary files in a directory is using the «locate» command.

Method 4: Using the «locate» command

  • Step 1 — Open a terminal on your Linux machine.
  • Step 2 — Run the command «locate -b ‘\binary'»

This command uses the locate command to search the database of files on the system for files that have the word «binary» in their name. The «-b» option is used to search for files that have the search term in their base name (i.e., the name of the file without the path).

It’s important to note that the locate command uses a database of files on the system that is created and updated by the «updatedb» command. So if the locate command does not return the expected result, you may need to run «updatedb» command to update the database before running the locate command.

Another important thing to note is that these commands will search for binary files in the entire system. If you want to search for binary files in a specific directory, you can specify the path to that directory with the command (example: locate -b ‘\binary’ /path/to/directory).

Conclusion

In conclusion, finding binary files in a directory on Linux can be done using several different commands such as «file», «find» and «locate» each of these commands have their own specific options and parameters that can be used to search for binary files in a specific directory or the entire system. The «file» command can be used to search for binary files in the current directory and all subdirectories, the «find» command can be used to search for binary files in the current directory and all subdirectories, with different options and techniques to filter the output and the «locate» command can be used to search for binary files in the entire system. It’s important to note that if you use the locate command, it is important to update the database before running the command if needed. As you practice more and more, you will be able to choose the right command and options to suit your needs.

Читайте также:  Что такое pipeline linux

Источник

How to find all binary executables recursively within a directory?

all executable files are listed (excluding directories), and including executable script file (like script.sh, etc). What I want to do is list only binary executable files.

4 Answers 4

You might try the file utility. According to the manpage:

The magic tests are used to check for files with data in particular fixed formats. The canonical example of this is a binary executable (compiled program) a.out file, whose format is defined in , and possibly in the standard include directory.

You might have to play around with the regular expression but something like:

$ find -type f -executable -exec file -i '<>' \; | grep 'x-executable; charset=binary' 

file has lots of options, so you might want to take a closer look at the man page. I used the first option I found that seemed to output easily-to-grep output.

I’d say use find -type f -executable -exec sh -c «file -i ‘<>‘ | grep -q ‘x-executable; charset=binary'» \; -print . It will only give you files (and thus can be passed to the next command he wants to run)

serverfault.com/a/584595/211551 solution finds files that are NOT marked executable but are executable.

On OS X, you can install GNU find with brew install findutils or sudo port install findutils and then you can run an invocation like this to a similar effect: gfind . -type f -executable -exec file ‘<>‘ \; | grep -i execut

Here’s a way to exclude scripts, i.e., files whose first two characters are #! :

find -type f -executable -exec sh -c 'test "$(head -c 2 "$1")" != "#!"' sh <> \; -print 

For some kinds of files, it’s not clear whether you want them classified as scripts or binary, for example bytecode files. Depending on how things are set up, these may or may not start with #! . If these matter to you, you’ll have to make the inner shell script more complex. For example, here’s how you might include ELF binaries and Mono executables and Objective Caml bytecode programs but not other kinds of executables like shell scripts or perl scripts or JVM bytecode programs:

find -type f -executable -exec sh -c ' case "$(head -n 1 "$1")" in ?ELF*) exit 0;; MZ*) exit 0;; #!*/ocamlrun*) exit 0;; esac exit 1 ' sh <> \; -print 

Источник

Оцените статью
Adblock
detector