Difference between revisions 508014 and 508015 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(contracted; show full)<code>-empty</code> or <code>-name</code>. find /foo -delete # this deletes '''all''' in /foo ===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 =type f -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 -type f -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/dev/null '{}' \+ </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'''*' If the <code>-iname</code> switch is not supported on your system then workaround techniques may be possible such as: (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]] 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=508015.
![]() ![]() 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.
|