Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Find (Unix)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Find syntax== <syntaxhighlight lang="console"> $ find [-H|-L] path... [operand_expression...] </syntaxhighlight> The two options control how the <code>find</code> command should treat symbolic links. The default behaviour is never to follow symbolic links. The {{code|-L}} flag will cause the <code>find</code> command to follow symbolic links. The {{code|-H}} flag will only follow symbolic links while processing the command line arguments. These flags are specified in the POSIX standard for <code>find</code>.<ref name=sus>{{man|cu|find|SUS|find files}}</ref> A common extension is the {{code|-P}} flag, for explicitly disabling symlink following.<ref name=fbsd>{{man|1|find|FreeBSD}}</ref><ref>{{man|1|find|Linux}}</ref> At least one path must precede the expression. <code>find</code> is capable of interpreting [[Wildcard character|wildcards]] internally and commands must be quoted carefully in order to control [[Glob (programming)|shell globbing]]. Expression elements are separated by the [[command-line argument]] boundary, usually represented as whitespace in shell syntax. They are evaluated from left to right. They can contain logical elements such as AND ({{code|-and}} or {{code|-a}}) and OR ({{code|-or}} or {{code|-o}}) as well as predicates (filters and actions). GNU <code>find</code> has a large number of additional features not specified by POSIX. ===Predicates=== {{anchor|type filters}} Commonly-used primaries include: * <code>-name ''pattern''</code>: tests whether the file name matches the shell-glob pattern given. * <code>-type ''type''</code>: tests whether the file is a given type. [[Unix file types]] accepted include: ** <code>b</code>: [[Device file|block device (buffered)]]; ** <code>c</code>: [[Device file|character device (unbuffered)]]; ** <code>d</code>: ''[[Directory (computing)|directory]]''; ** <code>f</code>: ''[[regular file]]''; ** <code>l</code>: [[symbolic link]]; ** <code>p</code>: [[named pipe]]; ** <code>s</code>: [[Unix domain socket|socket]]; ** <code>D</code>: [[Doors (computing)|door]]. * <code>-print</code>: always returns true; prints the name of the current file plus a newline to the [[stdout]]. * <code>-print0</code>: always returns true; prints the name of the current file plus a null character to the [[stdout]]. Not required by POSIX. * <code>-exec ''program'' ''[arguments...]'' ;</code>: runs ''program'' with the given arguments, and returns true if its exit status was 0, false otherwise. If ''program'', or an argument is '''{}''', it will be replace by the current path (if ''program'' is '''{}''', <code>find</code> will try to run the current path as an executable). POSIX doesn't specify what should happen if multiple '''{}''' are specified. Most implementations will replace all '''{}''' with the current path, but that is not standard behavior. * <code>-exec ''program'' ''[arguments...]'' {} +</code>: always returns true; run ''program'' with the given arguments, followed by as many paths as possible (multiple commands will be run if the maximum command-line size is exceeded, like for [[xargs]]).<ref name=sus/> * <code>-ok ''program'' ''[arguments...]'' ;</code>: for every path, prompts the user for confirmation; if the user confirms (typically by entering ''y'' or ''yes''), it behaves like <code>-exec ''program [arguments...]'' ;</code>, otherwise the command is not run for the current path, and false is returned. * <code>-maxdepth</code>: Can be used to limit the directory depth to search through. For example, <code>-maxdepth 1</code> limits search to the current directory. If the expression uses none of <code>-print0</code>, <code>-print</code>, <code>-exec</code>, or <code>-ok</code>, find defaults to performing <code>-print</code> if the conditions test as true. ===Operators=== Operators can be used to enhance the expressions of the find command. Operators are listed in order of decreasing precedence: * <code>( expr )</code>: forces precedence; * <code>! expr</code>: true if {{code|expr}} is false; * <code>expr1 expr2</code> (or <code>expr1 -a expr2</code>): AND. {{code|expr2}} is not evaluated if {{code|expr1}} is false; * <code>expr1 -o expr2</code>: OR. {{code|expr2}} is not evaluated if {{code|expr1}} is true. <syntaxhighlight lang="console"> $ find . -name 'fileA_*' -o -name 'fileB_*' </syntaxhighlight> This command searches the current working directory tree for files whose names start with {{code|fileA_}} or {{code|fileB_}}. We quote the {{code|fileA_*}} so that the shell does not expand it. <syntaxhighlight lang="console"> $ find . -name 'foo.cpp' '!' -path '.svn' </syntaxhighlight> This command searches the current working directory tree except the subdirectory tree ".svn" for files whose name is "foo.cpp". We quote the <code>!</code> so that it's not interpreted by the shell as the history substitution character.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)