Difference between revisions 508016 and 508017 on knwiki

{{other uses}}
{{unreferenced|date=September 2013}}
{{lowercase|title=find}} 
In [[Unix-like]] and some other [[operating system]]s, <code>'''find'''</code> is a [[command-line utility]] that [[Search engine (computing)|searches]] through one or more [[directory tree]]s of a [[file system]], locates [[Computer file|file]]s based on some [[user (computing)|user]]-specified criteria and applies a user-specified action on each matched file. The possible search criteria include a [[pattern matching|pattern]] to match against the [[file name]] or a time range to match against the modification time or access time of the file. By default, <code>find</code> returns a list of all files below the current [[working directory]].

The related <code>[[locate (Unix)|locate]]</code> programs use a database of indexed files obtained through <code>find</code> (updated at regular intervals, typically by <code>[[cron]]</code> job) to provide a faster method of searching the entire filesystem for files by name.  

== Find syntax ==
{{expand section|date=August 2008}}

<code>find [-H] [-L] [-P] path... [expression]</code>

The three options control how the <code>find</code> command should treat symbolic links. The default behaviour is never to follow symbolic links. This can be explicitly specified using the -P flag. The -L flag will cause the <code>find</code> command to follow symbolic links. The -H flag will only follow symbolic links while processing the command line arguments. These flags are not available with some older versions of <code>find</code>.

At least one path must precede the expression. <code>find</code> is capable of interpreting [[Wildcard character|wildcards]] internally and commands must be constructed carefully in order to control [[Glob (programming)|shell globbing]].

Expression elements are whitespace-separated and evaluated from left to right.  They can contain logical elements such as AND (&#x2011;and or &#x2011;a) and OR (&#x2011;or or &#x2011;o) as well as more complex predicates.

The [[GNU Find Utilities|GNU]] <code>find</code> has a large number of additional features not specified by POSIX.

== POSIX protection from infinite output ==

Real-world filesystems often contain looped structures created through the use of [[hard link|hard]] or [[symbolic link|soft links]].  The [[POSIX|POSIX standard]] requires that
{{Quotation|
The <code>find</code> utility shall detect infinite loops; that is, entering a previously visited
directory that is an ancestor of the last file encountered. When it detects an infinite
loop, <code>find</code> shall write a diagnostic message to standard error and shall either recover
its position in the hierarchy or terminate.
}}


==Operators ==
Operators can be used to enhance the expressions of the find command. Operators are listed in order of decreasing precedence:

*'''( expr )''' Force precedence. 
*'''! expr''' True if expr is false. 
*'''-not expr''' Same as ! expr. 
*'''expr1 expr2''' And (implied); expr2 is not evaluated if expr1 is false. 
*'''expr1 -a expr2''' Same as expr1 expr2. 
*'''expr1 -and expr2''' Same as expr1 expr2. 
*'''expr1 -o expr2''' Or; expr2 is not evaluated if expr1 is true. 
*'''expr1 -or expr2''' Same as expr1 -o expr2 but doesn't work as widely (since POSIX doesn't require it). 
*'''expr1  , expr2''' List; both expr1 and expr2 are always evaluated. The value of expr1 is discarded (though its side-effects occur); the value of the list is the value of expr2.  

 find . -name 'fileA_*' -o -name 'fileB_*'

This command searches files whose name has a prefix of "fileA_" or "fileB_" in the current directory.

 find . -name 'foo.cpp' '!' -path '.svn'

(contracted; show full)
* '''l '''[[symbolic link]]; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype (though that is a GNU extension).
* '''s '''[[Unix domain socket|socket]]
* '''D '''[[Doors (computing)|door (Solaris)]]
(Bold listed configuration switches are most commonly used)

==Examples==



===From current directory===
 find . -name 'my*'

This searches in the current directory (represented by the dot character) and below it, for files and directories with names starting with ''my''. The quotes avoid the [[shell (computing)|shell]] expansion — without them the shell would replace ''my*'' with the list of files whose names begin with ''my'' in the current directory. In newer versions of the program, the directory may be omitted, and it will imply the current directory.

===Files only===
 find . -name 'my*' -type f
This limits the results of the above search to only regular files, therefore excluding directories, special files, pipes, symbolic links, etc. ''my*'' is enclosed in single quotes (apostrophes) as otherwise the shell would replace it with the list of  files in the current directory starting with ''my''......

===Commands===
The previous examples created listings of results because, by default, <code>find</code> executes the '-print' action.   (Note that early versions of the <code>find</code> command had no default action at all; therefore the resulting list of files would be discarded, to the bewilderment of users.)  

 find . -name 'my*' -type f -ls
This prints extended file information.

===Search all directories===
 find / -name myfile -type f -print
This searches every file on the computer for a file with the name ''myfile'' and prints it to the screen. It is generally not a good idea to look for data files this way.  This can take a considerable amount of time, so it is best to specify the directory more precisely.  Some operating systems may mount dynamic filesystems that are not cong(contracted; show full)
Example of searching non-empty files.
 find . -not -size 0k

===Search files by name and size ===
 '''find''' /usr/src {{abbr|-not|the negation of the expression that follows}} {{abbr|\(|the start of a complex expression.}} -name '*,v' {{abbr|-o|a logical or of a complex expression. In this case the complex expression is all files like '*,v' or '.*,v'}} -name '.*,v' {{abbr|\)|the end of a complex expression.}} '{}' \; -print
  

This command will search in the /usr/src directory and all sub directories. All files that are of the form '*,v' and '.*,v' are excluded. Important arguments to note are in the [[tooltip]] that is displayed on mouse-over.

<source lang="bash" enclose="div">
for file in `find /opt \( -name error_log -o -name 'access_log' -o -name 'ssl_engine_log' -o -name 'rewrite_log' -o
(contracted; show full)*{{man|1|find||search for files in a directory hierarchy}}
*[http://www.gnu.org/software/findutils/manual/html_mono/find.html Official webpage for GNU find]

{{Unix commands}}

[[Category:Searching]]
[[Category:Standard Unix programs]]
[[Category:Unix SUS2008 utilities]]