|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
is it possible to select all executable files from a directory tree? I tried using find with -exec but i couldn't make it work find -type f -exec if test -x "{}" then echo "found an executable" fi \; i tried making a function like this function Qexe () { if test -x "$1" then echo "found an executable" fi } and then call it from find like this find -type f -exec Qexe "{}" \; but again it didn't work. thanks in advance for your , -nicolas PS: i think that my question could be generalized if it is possible to you your own predicate in find... |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
man find
then go to the -perm option there are also some examples in the examples section |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
> All your examples use -type f, which finds *all* the regular files, not
> just executables. > To find executables, you can use the -perm option: > > find -type f -perm /u+x,g+x,o+x > > (or any variation therein - for example, if you want only > owner-executables etc.) > > man find for the details. thank you guys for your answers, i did read the manual... the problem is that i would like to use my predicate-function and i can't get it to work. Is this possible? thanks for your time, -nicolas |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Aggelidis Nikos wrote:
> Hi, > > is it possible to select all executable files from a directory tree? > > I tried using find with -exec but i couldn't make it work All your examples use -type f, which finds *all* the regular files, not just executables. To find executables, you can use the -perm option: find -type f -perm /u+x,g+x,o+x (or any variation therein - for example, if you want only owner-executables etc.) man find for the details. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Aggelidis Nikos wrote:
> thank you guys for your answers, i did read the manual... the problem is > that i would like to use my predicate-function and i can't get it to > work. Is this possible? Ah, then I probably misunderstood your question in the first place. You want to find all files, and filter out the executables using a function or predicate of yours. (The following is tested under bash) Well, you can use an external script: $ cat ckexec.sh #!/bin/bash if [ -x "$1" ]; then echo "$1 is executable" else echo "$1 is not executable" fi $ ls -l total 20 -rwxr-xr-x 1 pk users 98 2008-03-13 15:58 ckexec.sh -rwxr-xr-x 1 pk users 8 2008-03-13 09:10 configure -rwxr-xr-x 1 pk users 22 2008-03-13 14:14 f -rwxr-xr-x 1 pk users 23 2008-03-13 14:14 f2 -rw-r--r-- 1 pk users 2164 2005-12-18 17:46 README $ find -type f -exec ckexec.sh '{}' \; ../README is not executable ../configure is executable ../f is executable ../f2 is executable ../ckexec.sh is executable Apparently, -exec does not accept a direct function name (but I stand to be corrected): $ function ckexec { if [ -x "$1" ]; then echo "$1 is executable"; else echo "$1 is not executable"; fi; } $ export -f ckexec $ find -type f -exec ckexec '{}' \; find: ckexec: No such file or directory find: ckexec: No such file or directory find: ckexec: No such file or directory find: ckexec: No such file or directory find: ckexec: No such file or directory Instead, you can use almost arbitrary code by using bash -c: $ find -type f -exec bash -c "ckexec '{}'" \; ../README is not executable ../configure is executable ../f is executable ../f2 is executable ../ckexec.sh is executable or even $ find -type f -exec bash -c "if [ -x '{}' ]; then echo '{}' is executable; else echo '{}' is not executable; fi;" \; ../README is not executable ../configure is executable ../f is executable ../f2 is executable ../ckexec.sh is executable |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
> Instead, you can use almost arbitrary code by using bash -c:
> thank you for your . i wasn't aware of bash -c and it really me complete my task! |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Mar 13, 7:36 pm, Aggelidis Nikos <aggelidis.n...@gmail.com> wrote:
> Hi, > > is it possible to select all executable files from a directory tree? > > I tried using find with -exec but i couldn't make it work > > find -type f -exec > if test -x "{}" > then > echo "found an executable" > fi > \; > > i tried making a function like this > > function Qexe () > { > if test -x "$1" > then > echo "found an executable" > fi > > } > > and then call it from find like this > > find -type f -exec Qexe "{}" \; > > but again it didn't work. > > thanks in advance for your , > -nicolas > > PS: i think that my question could be generalized if it is possible to > you your own predicate in find... find . \ -type f \ \( -perm -u+x -o -perm -g+x -o -perm -o+x \) \ -exec echo {} is executable \; \ -prune \ -o \ -type f \ -exec echo {} is not executable \; |
|
![]() |
| Outils de la discussion | |
|
|