Difference between revisions 507989 and 507990 on knwiki

{{other uses}}
{{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(contracted; show full)
 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. 
*'''expr1 , expr2''' List; both expr1 and expr2 are always evaluated. The value of expr1 is discarded; the value of the list is the value of expr2. 

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

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

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

This command searches for files with the name "foo.cpp" in all subdirectories of the current directory (current directory itself included) other than ".svn".

==Type filter explanation==
Various type filters are supported by find, they are activated using the

 find -type c

configuration switch where c may be any of:
* '''b '''[[Device file|block (buffered) special]]
* '''c '''[[Device file|character (unbuffered special)]]
* '''d [[Directory (computing)|directory]]'''
* '''p '''[[Named pipe|named pipe (FIFO)]]
* '''f [[regular file]]'''
* '''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.
* '''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 (contracted; show full)
 -name 'catalina.out' \) -size +300000k -a -size -5000000k`; do cat /dev/null > $file; done

The units should be one of [bckw], 'b' means 512-byte blocks, 'c' means byte, 'k' means kilobytes and 'w' means 2-byte words. The size does not count indirect blocks, but it does count blocks in sparse files that are not actually allocated.


===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. 
*'''expr1 , expr2''' List; both expr1 and expr2 are always evaluated. The value of expr1 is discarded; the value of the list is the value of expr2. 

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

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

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

This command searches for files with the name "foo.cpp" in all subdirectories of the current directory (current directory itself included) other than ".svn".

===Type filter explanation ===
Various type filters are supported by find, they are activated using the

 find -type c

configuration switch where c may be any of:
* '''b '''[[Device file|block (buffered) special]]
* '''c '''[[Device file|character (unbuffered special)]]
* '''d [[Directory (computing)|directory]]'''
* '''p '''[[Named pipe|named pipe (FIFO)]]
* '''f [[regular file]]'''
* '''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.
* '''s '''[[Unix domain socket|socket]]
* '''D '''[[Doors (computing)|door (Solaris)]]
(Bold listed configuration switches are most commonly used)
==See also==
*[[Locate_(Unix)|locate]], a Unix search tool based on a prebuilt database, and therefore it is faster and less accurate than <code>find</code> (because the database may not be up-to-date).
*[[mdfind]], a similar utility that utilizes metadata for [[Mac OS X]] and [[Darwin (operating system)|Darwin]]
*[[List of Unix programs]]
*[[List of DOS commands]]
*[[grep]]
*[[find (command)]], a DOS and Windows command that is very different from UNIX <code>find</code>
*[[findutils]]
*[[Tree (Unix)| tree]]

==External links==
*{{man|cu|find|SUS|find files}}
*{{man|1|find||search for files in a directory hierarchy}}
*[http://doc.cat-v.org/unix/find-history A story on the origins of the Unix find command].
*[http://www.gnu.org/software/findutils/ GNU Findutils] - Comes with the [[xargs]] and [[GNU locate|locate]] commands.
*[http://www.gnu.org/software/findutils/manual/html_mono/find.html Official webpage for GNU find]
*[http://www.softpanorama.org/Tools/Find/find_mini_tutorial.shtml Softpanorama find tutorial]
*[http://www.enciclopedia.galeon.com/find.html Exercises "Find"]
*[http://find.unixpin.com/ "Find helper" - unix "find" wizard]
*[http://www.oracle.com/technetwork/articles/calish-find-087766.html Guide to Linux Find Command Mastery]
*[http://www.shell-fu.org/lister.php?tag=find Top 'find' commands - interesting usage]

{{Unix commands}}

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