Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I list all binary executable files recursively within a directory?

I use find command like this:

find /mydir/mysubdir -executable -type f

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

share|improve this question
Please change the subject to include the word 'binary', which is key to answering your question. – Stefan Lasiewski Sep 2 '10 at 3:43
up vote 24 down vote accepted

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.

share|improve this answer
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) – Gert Sep 2 '10 at 6:56

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;;
    exit 1
' sh {} \; -print
share|improve this answer

Just in case you find yourself on a system with a downlevel find (there are still, as I write, a lotta science clusters running RHEL5!) without the rights to update: instead of

find /mypath/ -executable -type f

in the above excellent answers, you can do, e.g.,

find /mypath/h -type f -perm -u+x

which searches on permission bits. Unfortunately the above only finds files for which the user has executable, but that usually works for me.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.