How to Find binary files with or without executable permissions?
I am working on a BASH script that would list in a path:
* Shared Object files,
* scripts, executable files,
* static libraries
It should not display intermediate files like .o (object files).
-
You mean find all binary ELF files that don't have the execute bit?– Noam RathausCommented Nov 25, 2013 at 12:16
-
Yes, nrathaus, I have files mixed up under number of directories (like in a tree), they do not have execute permissions: -rw-rw-r--, I know they contain executables, How to find / list executables?– AjayKumarBasuthkarCommented Nov 25, 2013 at 12:18
-
Welcome to StackOverflow! S.O. provides a mechanism whereby you can ask for help to fix errors in your code. What have you tried?– ghotiCommented Nov 25, 2013 at 12:51
-
Hi ghoti, Thanks for the warm welcome. I have a bash script, I am looking to display or list files in a directory hierarchy that are actually executable files and they are not marked executable, ya let the script list if they are marked executable as well :)– AjayKumarBasuthkarCommented Nov 25, 2013 at 13:05
-
Hi gothi & nrathaus, Is there any thing I could do to get +1? Thanks for your support.– AjayKumarBasuthkarCommented Nov 26, 2013 at 5:52
Add a comment
|
3 Answers
You can use:
find . -type f -print0 | xargs -0 -n 10 file -i | grep "application/x-executable"
-
BTW: Its taken from here: unix.stackexchange.com/questions/40063/… Commented Nov 25, 2013 at 12:21
-
-
It runs relative to your path, go to "/" before running it Commented Nov 25, 2013 at 12:30
-
It also doesn't filter out those that are already marked as "executable" Commented Nov 25, 2013 at 12:31
-
Okay, I should list files those that are marked executable for example shared object files .so and others. Commented Nov 25, 2013 at 12:40
The file
program reports file type based on contents, so
file /bin/* | awk -F: '/executable/{print $1}'
reports potentially/actually executable files in /bin.
-
I wish to list all the files in hierarchy. I tried: find ./ -name "*" | xargs file | awk -F: '/executable/{print $1}' Not effective. Commented Nov 25, 2013 at 12:45
-
I tried the find | xargs | awk pipeline just now, and every file it reported is in some executable format.– arntCommented Nov 25, 2013 at 12:54
-
And what does file report for these files? See if you can spot a pattern, and change the /foo/ argument to awk.– arntCommented Nov 25, 2013 at 21:25
-
Hi arnt, Is there any thing I could do to get +1? Thanks for your support. Commented Nov 26, 2013 at 5:54
-
find . -type f | xargs file | awk -F: '/script/{print $1} /ELF/{print $1}' perhaps?– arntCommented Nov 26, 2013 at 8:01
This worked for me & thought of sharing...
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