Difference between revisions 507899 and 507900 on knwiki

{{otheruses}}
{{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)

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

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

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

At least one path must precede the expression.  Find<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 (-a) and OR (-o) as well as more complex predicates.

The [[GNU findutils|GNU]] find<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
 The find<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, find<code>find</code> shall write a diagnostic message to standard error and shall either recover
 its position in the hierarchy or terminate.

==Examples==

===From current directory===
 find . -name 'my*'
(contracted; show full)
This prints extended file information.

===Search all directories===
 find / -type f -name "myfile" -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 congenial to 
find<code>find</code>.

===Search all but one directory subtree===
 find / -path excluded_folder -prune -o -type f -name myfile -print
This searches every folder on the computer except the subtree ''excluded_folder'' for a file with the name ''myfile''.  It will not detect directories, devices, links, doors, or other "special" filetypes.

===Specify a directory===
 find /home/weedly -name "myfile" -type f -print
This searches for files named ''myfile'' in the ''/home/weedly'' directory, the home directory for userid ''weedly''.  You should always specify the directory to the deepest level you can remember.

===Search several directories===
 find local /tmp -name mydir -type d -print
This searches for directories named ''mydir'' in the ''local'' subdirectory of the current working directory and the ''/tmp'' directory.

===Ignore errors===
If you're doing this as a user other than root, you might want to ignore permission denied (and any other) errors.  Since errors are printed to [[stderr]], they can be suppressed by redirecting the output to /dev/null.  The following example shows how to do this in the bash shell: 
 find / -name "myfile" -type f -print 2>/dev/null

If you are a [[C shell|csh]] or [[tcsh]] user, you cannot redirect [[stderr]] without redirecting [[stdout]] as well.  You can use sh to run the find<code>find</code> command to get around this:
 sh -c find / -name "myfile" -type f -print 2>/dev/null

An alternate method when using [[C shell|csh]] or [[tcsh]] is to pipe the output from [[stdout]] and [[stderr]] into a [[grep]] command. This example shows how to suppress lines that contain permission denied errors.
 find . -name "myfile" |& grep -v "Permission denied"

===Find any one of differently named files===
(contracted; show full)

Note that the command itself should *not* be quoted; otherwise you get error messages like

 find: echo "mv ./3bfn rel071204": No such file or directory

which means that 
'''find'''<code>find</code> is trying to run a file called 'echo "mv ./3bfn rel071204"' and failing.

If running under Windows, don't include the backslash before the semicolon:

 find . -exec grep blah {} ;

If you will be executing over many results, it is more efficient to pipe the results to the [[xargs]] command instead.  xargs is a more modern implementation, and handles long lists in a more intelligent way.  The print0 option can be used with this.

The following command will ensure that filenames with whitespaces are passed to the executed COMMAND without being split up by the shell.  It looks complicated at first glance, but is widely used.

 find . -print0 | xargs -0 COMMAND

The list of files generated by find<code>find</code> (whilst it is being generated) is simultaneously [[Pipe_(Unix)|piped]] to xargs, which then executes COMMAND with the files as arguments.  See [[xargs]] for more examples and options.

===Search for a string===
This command will search for a string in all files from the /tmp directory and below:

 find /tmp -exec grep "search string" '{}' /dev/null \; -print

(contracted; show full)
 echo "'MyFile*'" |perl -pe 's/([a-zA-Z])/[\L\1\U\1]/g;s/(.*)/find . -name \1/'|sh

===Search files by size===
Example of searching files with size between 100 kilobytes and 500 kilobytes.
 find . -size +100k -a -size -500k




===Search files by name and size ===
  for file in `find /opt \( -name error_log -o -name 'access_log' -o -name 'ssl_engine_log' -o -name 'rewrite_log' -o -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. 

==See also==
*[[GNU locate]], a Unix search tool based on a prebuilt database therefore faster and less accurate than find<code>find</code>
*[[mdfind]], a similar utility that utilizes metadata for [[Mac OS X]] and [[Darwin (operating system)|Darwin]]
*[[List of Unix programs]]
*[[List of DOS commands]]
*[[find (command)]], a DOS and Windows command that is very different from UNIX find<code>find</code>
*[[findutils]]

==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/technology/pub/articles/calish-find.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]]

[[de:Find]]
[[el:Find]]
[[es:Find]]
[[fr:Find]]
[[it:Find (Unix)]]
[[hu:Find]]
[[ja:Find]]
[[pl:Find]]
[[pt:Find]]
[[ro:Find]]
[[ru:Find]]
[[fi:Find (Unix)]]