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

08
2014-07
  • HaiYuan Zhang

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

  • Answers
  • knittl

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

  • innaM

    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.

  • AjayKumarBasuthkar

    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
    

  • Related Question

    linux - How can I search directories starting with a certain letter?
  • bing

    How can I search through directories starting with a certain letter with the Linux find command.

    For example I want to search all directories starting with the letter a for a file or directory starting with b.


  • Related Answers
  • John T

    Try a find in a find:

    find . -type d -name "a*" -exec find {} -name "b" \;

    Starting at the current directory (.), find will look for all directories starting with the letter a recursively. For each directory it finds, it will look inside it for a file named b.

    If you only want it to look in the folders starting with a, and no directories in those a* folders, use maxdepth:

    find . -type d -name "a*" -exec find {} -maxdepth 1 -name "b" \;

    to get rid of errors:

    find . -type d -name "a*" 2> /dev/null -exec find {} -maxdepth 1 -name "b" \;
  • bing

    Just a quick update for people who might end up on this question.

    In addition to the solution John T provided, I have also found that you can exclude directories by using the prune switch (should have read the man pages sooner I guess, hehe.)

    So for example if I want to search all directories for file or directory "b" except directories starting with an "a" I can do this

    find . -path 'a*' -prune -o -name "b" -print
    

    bing

  • user23307

    you can also use find -regex...

    find -regex .*/a.*/b