Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How to find the executable files under a certain directory in linux?

share|improve this question

migrated from stackoverflow.com Sep 10 '09 at 12:46

This question came from our site for professional and enthusiast programmers.

    
Linx? Or Linux? –  Wim ten Brink Sep 10 '09 at 12:01
    
@WimtenBrink GNU/Linux ! :p –  Shadok Feb 23 '12 at 14:31
    
@HaiYuan Zhang - is there anything wrong with knittl's answer? It worked for me. Since you did not accept it, I'm wondering what's wrong with it (and what I might be missing here). –  noloader Mar 22 at 3:43
    
Here is a kind of BASH script, it is not-bad is what I can say :) stackoverflow.com/a/20209457/2067125 –  AjayKumarBasuthkar Mar 26 at 7:49

3 Answers 3

use the -executable option:

find <dir> -executable

if you want to find only executable files and not searchable directories, combine with -type f:

find <dir> -executable -type f

EDIT:

checking with the comments i see there’s no type x. i’m sorry, this was my mistake. checking for executable files can be done with -perm (not recommended) or -executable (recommended, as it takes ACL into account).

share|improve this answer
    
This will return files with the execute turned on only. A more thorough analysis would check for a shebang line or whether the file is binary –  ennuikiller Sep 10 '09 at 12:04
15  
a shebang doesn’t mean they’re executable. it tells us only which interpreter to use. and by linux definition “executable files” are files with the executable (x) bit set –  knittl Sep 10 '09 at 12:09
2  
What version of find supports that type for -type? man find lists b, c, d, p, f, l, s and D on my system. –  innaM Sep 10 '09 at 15:51
1  
Same here, my find doesn't have a -type x either. –  davr Sep 10 '09 at 17:17
4  
If you have an old version of find (probably before 4.3.8) which lacks -executable use find . -perm /u=x,g=x,o=x. –  Ludwig Weinzierl May 14 '10 at 19:06

Use the find's -perm option. This will find files in the current directory that are either executable by their owner, by group members or by others:

find . -perm /u=x,g=x,o=x

Edit:

I just found another option that is present at least in GNU find 4.4.0:

find . -executable

This should work even better because ACLs are also considered.

share|improve this answer
    
This only works on a newer version of find. The one that comes by default with CentOS gives the error find: invalid mode /u=x,g=x,o=x'` –  davr Sep 10 '09 at 17:18
6  
Then you should try the "-perm +" version which is now deprecated in GNU find: find . -perm +111" –  innaM Sep 10 '09 at 19:32

A file marked executable need not be a executable or loadable file or object.

Here is what I use:

find ./ -type f -name "*" -not -name "*.o" -exec sh -c '
    case "$(head -n 1 "$1")" in
      ?ELF*) exit 0;;
      MZ*) exit 0;;
      #!*/ocamlrun*)exit0;;
    esac
exit 1
' sh {} \; -print
share|improve this answer

Your Answer

 
discard

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.