Difference between revisions 507993 and 507994 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 [[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. (contracted; show full) * '''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*'<ref> Note that for RedHat Linux Version 9: <pre>$ find . -name my* find: paths must precede expression.</pre> Double quotes find . -name "my*" works fine. </ref> 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. Note that for RedHat Linux Version 9: find . -name my* returns this error '''find: paths must precede expression.''' Double quotes find . -name "my*" works fine.⏎ ⏎ ===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=== (contracted; show full)'''Warning''': <code>-delete</code> should be use with other operators such as <code>-empty</code> or <code>-name</code>. find /foo -delete (this deletes '''all''' in <code>foo</code>) ===Search for a string=== This command will search for a string in all files from the /tmp directory and below: ⏎ ⏎ <source lang="bash"> $ find /tmp -exec grep 'search string' '{}' /dev/null \; -print </source> The <tt>[[/dev/null]]</tt> argument is used to show the name of the file before the text that is found. Without it, only the text found is printed. An equivalent mechanism is to use the "-H" or "--with-filename" option to grep: ⏎ ⏎ <source lang="bash"> $ find /tmp -exec grep -H 'search string' '{}' ';' -print </source> GNU grep can be used on its own to perform this task: $ grep -r 'search string' /tmp Example of search for "LOG" in jsmith's home directory <source lang="bash" highlight="1"> $ find ~jsmith -exec grep LOG '{}' /dev/null \; -print /home/jsmith/scripts/errpt.sh:cp $LOG $FIXEDLOGNAME /home/jsmith/scripts/errpt.sh:cat $LOG /home/jsmith/scripts/title:USER=$LOGNAME </source> Example of search for the string "ERROR" in all XML files in the current directory and all sub-directories <source lang="bash"> $ find . -name "*.xml" -exec grep "ERROR" '{}' \; -print </source> The double quotes (" ") surrounding the search string and single quotes (<nowiki>' '</nowiki>) surrounding the braces are optional in this example, but needed to allow spaces and some other special characters in the string. Note with more complex text (notably in most popular shells descended from `sh` and `csh`) single quotes are often the easier choice, since '''double quotes do not prevent all special interpretation'''. Quoting filenames which have English contractions demonstrates how this can get rather complicated, since a string with an apostrophe in it is easier to protect with double quotes. Example: <source lang="bash"> $ find . -name "file-containing-can't" -exec grep "can't" '{}' \; -print </source> ===Search for all files owned by a user=== find . -user <userid> ===Search in case insensitive mode=== find . -iname 'MyFile*' -iname, not to be confused with -inum( find based on inode value) (contracted; show full) 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 -name 'catalina.out' \) -size +300000k -a -size -5000000k`; do ⏎ ⏎ cat /dev/null > $file; ⏎ ⏎ done </source> 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== *[[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] - <ref>Comes with the [[xargs]] and [[GNU locate|locate]] commands.</ref> *[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] == 주석 == <references />⏎ {{Unix commands}} [[Category:Searching]] [[Category:Standard Unix programs]] [[Category:Unix SUS2008 utilities]] All content in the above text box is licensed under the Creative Commons Attribution-ShareAlike license Version 4 and was originally sourced from https://kn.wikipedia.org/w/index.php?diff=prev&oldid=507994.
![]() ![]() This site is not affiliated with or endorsed in any way by the Wikimedia Foundation or any of its affiliates. In fact, we fucking despise them.
|