Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the executable files in the current directory and find out their extensions?

Tags:

find

bash

I need to find all executable files from /bin. How to do it using

find . -executable

and how to check if the file is script (for example, sh, pl, bash)?

like image 776
DmitryB Avatar asked Dec 03 '25 02:12

DmitryB


2 Answers

#!/bin/bash                                                                                                                                                    

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

and even better to do

find /bin -type f -perm +111 -print0 | xargs -0 file
like image 100
alexander Avatar answered Dec 05 '25 17:12

alexander


find /bin/ -executable returns all executable files from /bin/ directory.

To filtering extension there are usable -name flag. For example, find /bin/ -executable -name "*.sh" returns sh-scripts.

UPD:

If file is not a binary file and do not have extension, it's possible to figured out it's type from shabang.

For example find ~/bin/ -executable | xargs grep --files-with-matches '#!/bin/bash' returns files from ~/bin/ directory, which contains #!/bin/bash.

like image 29
ДМИТРИЙ МАЛИКОВ Avatar answered Dec 05 '25 15:12

ДМИТРИЙ МАЛИКОВ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!