What type of parameter/flag can I use with the Unix find
command so that I search executables?
相关问题
- JQ: Select when attribute value exists in a bash a
- Why should we check WIFEXITED after wait in order
- bash print whole line after splitting line with if
- “command not found” errors in expect script execut
- grep using grep results
相关文章
- Check if directory exists on remote machine with s
- Emacs/xterm color annoyance on Linux
- Making new files automatically executable?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
- Bash script that creates a directory structure
- Test if File/Dir exists over SSH/Sudo in Python/Ba
On GNU versions of find you can use
-executable
:For BSD versions of find, you can use
-perm
with+
and an octal mask:In this context "+" means "any of these bits are set" and 111 is the execute bits.
Note that this is not identical to the
-executable
predicate in GNU find. In particular,-executable
tests that the file can be executed by the current user, while-perm +111
just tests if any execute permissions are set.Older versions of GNU find also support the
-perm +111
syntax, but as of 4.5.12 this syntax is no longer supported. Instead, you can use-perm /111
to get this behavior.This worked for me & thought of sharing...
It is SO ridiculous that this is not super-easy... let alone next to impossible. Hands up, I defer to Apple/Spotlight...
mdfind 'kMDItemContentType=public.unix-executable'
At least it works!
I had the same issue, and the answer was in the dmenu source code: the stest utility made for that purpose. You can compile the 'stest.c' and 'arg.h' files and it should work. There is a man page for the usage, that I put there for convenience:
You can use the
-executable
test flag:Tip of the hat to @gniourf_gniourf for clearing up a fundamental misconception.
This answer attempts to provide an overview of the existing answers and to discuss their subtleties and relative merits as well as to provide background information, especially with respect to portability.
Finding files that are executable can refer to two distinct use cases:
Note that in either scenario it may make sense to use
find -L ...
instead of justfind ...
in order to also find symlinks to executables.Note that the simplest file-centric case - looking for executables with the executable permissions bit set for ALL three security principals (user, group, other) - will typically, but not necessarily yield the same results as the user-centric scenario - and it's important to understand the difference.
User-centric (
-executable
)The accepted answer commendably recommends
-executable
, IF GNUfind
is available.find
comes with most Linux distros-executable
matches only files the current user can execute (there are edge cases.[1]).The BSD
find
alternative offered by the accepted answer (-perm +111
) answers a different, file-centric question (as the answer itself states).-perm
to answer the user-centric question is impossible, because what is needed is to relate the file's user and group identity to the current user's, whereas-perm
can only test the file's permissions.Using only POSIX
find
features, the question cannot be answered without involving external utilities.Thus, the best
-perm
can do (by itself) is an approximation of-executable
. Perhaps a closer approximation than-perm +111
is-perm -111
, so as to find files that have the executable bit set for ALL security principals (user, group, other) - this strikes me as the typical real-world scenario. As a bonus, it also happens to be POSIX-compliant (usefind -L
to include symlinks, see farther below for an explanation):gniourf_gniourf's answer provides a true, portable equivalent of
-executable
, using-exec test -x {} \;
, albeit at the expense of performance.Combining
-exec test -x {} \;
with-perm +111
(i.e., files with at least one executable bit set) may help performance in thatexec
needn't be invoked for every file (the following uses the POSIX-compliant equivalent of BSD find-perm +111
/ GNU find-perm /111
; see farther below for an explanation):File-centric (
-perm
)-perm
primary (known as a test in GNU find terminology).-perm
allows you to test for any file permissions, not just executability.111
), whereas symbolic modes are strings (e.g.,a=x
).u
(user),g
(group) ando
(other), ora
to refer to all three. Permissions are expressed asx
for executable, for instance, and assigned to principals using operators=
,+
and-
; for a full discussion, including of octal modes, see the POSIX spec for thechmod
utility.find
:-
(e.g.,-ug=x
) means: match files that have all the permissions specified (but matching files may have additional permissions).755
) means: match files that have this full, exact set of permissions.+
/
[2]File-centric command examples
Note:
+
or/
.!
for NOT (GNU find and BSD find also allow-not
); note that\!
is used in the examples so as to protect!
from shell history expansions-a
for AND (GNU find and BSD find also allow-and
)-o
for OR (GNU find and BSD find also allow-or
)-
, the=
and+
operators can be used interchangeably (e.g.,-u=x
is equivalent to-u+x
- unless you apply-x
later, but there's no point in doing that).,
to join partial modes; AND logic is implied; e.g.,-u=x,g=x
means that both the user and the group executable bit must be set.-perm
expression with the NOT primary,!
.-print
, or-perm
; also known as actions and tests in GNU find) are implicitly joined with-a
(logical AND), and that-o
and possibly parentheses (escaped as\(
and\)
for the shell) are needed to implement OR logic.find -L ...
instead of justfind ...
is used in order to also match symlinks to executables-L
instructs find to evaluate the targets of symlinks instead of the symlinks themselves; therefore, without-L
,-type f
would ignore symlinks altogether.[1] Description of
-executable
fromman find
as of GNU find 4.4.2:[2] GNU find versions older than 4.5.12 also allowed prefix
+
, but this was first deprecated and eventually removed, because combining+
with symbolic modes yields likely yields unexpected results due to being interpreted as an exact permissions mask. If you (a) run on a version before 4.5.12 and (b) restrict yourself to octal modes only, you could get away with using+
with both GNU find and BSD find, but it's not a good idea.