2012-02-21 50 views

回答

4
#!/bin/bash                                      

for file in `find /bin` ; do                                 
    if [ -x $file ] ; then                                  
     file $file                                    
    fi                                       
done 

,甚至更好做

find /bin -type f -perm +111 -print0 | xargs -0 file 
1

find /bin/ -executable返回/bin/目錄中的所有可執行文件。

要過濾擴展名,有可用的-name標誌。例如,find /bin/ -executable -name "*.sh"返回sh -scripts。

UPD

如果文件不是二進制文件,並沒有擴展,它可以計算出從shabang它的類型。

例如find ~/bin/ -executable | xargs grep --files-with-matches '#!/bin/bash'返回​​目錄中的文件,其中包含#!/bin/bash

+0

謝謝,但如何確定腳本類型,如果文件沒有擴展名? – DmitryB 2012-02-21 20:18:58

+2

針對'#!/ bin/bash'的貼圖存在誤報的風險,而缺少使用/ bin/sh的文件。您最好使用'file'命令來確定文件的可執行文件類型。 – Kenster 2012-02-21 20:40:30

+0

對,shabang grepping有點「文件」響應。不知道,謝謝。 – 2012-02-21 20:47:49

0

這爲我工作&思想的共享......

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

要找到所有的腳本Shell

find . -type f -executable | xargs file -i | grep x-shellscript | cut -d":" -f1 

找到所有的可執行文件

find . -type f -executable | xargs file -i | grep x-exec | cut -d":" -f1 

要找到所有的共享庫

find . -type f -executable | xargs file -i | grep x-sharedlib | cut -d":" -f1 
相關問題