Search for executable files using find command

前端 未结 10 870
既然无缘
既然无缘 2020-12-07 11:21

What type of parameter/flag can I use with the Unix find command so that I search executables?

10条回答
  •  心在旅途
    2020-12-07 12:03

    find . -executable -type f
    

    does not really guarantee that the file is executable it will find files with the execution bit set. If you do

    chmod a+x image.jpg
    

    the above find will think image.jpg is an executable even if it is really a jpeg image with the execution bit set.

    I generally work around the issue with this:

    find . -type f -executable -exec file {} \; | grep -wE "executable|shared object|ELF|script|a\.out|ASCII text"
    

    If you want the find to actually print dome information about executable files you can do something like this:

    find . -type f -executable -printf "%i.%D %s %m %U %G %C@ %p" 2>/dev/null |while read LINE
    do
      NAME=$(awk '{print $NF}' <<< $LINE)
      file -b $NAME |grep -qEw "executable|shared object|ELF|script|a\.out|ASCII text" && echo $LINE
    done
    

    In the above example the file's full pathname is in the last field and must reflect where you look for it with awk "NAME=$(awk '{print $NF}' <<< $LINE)" if the file name was elsewhere in the find output string you need to replace "NF" with the correct numerical position. If your separator is not space you also need to tell awk what your separator is.

提交回复
热议问题