Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Find (Unix)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Search for a string=== This command will search all files from the /tmp directory tree for a string: <syntaxhighlight lang="console"> $ find /tmp -type f -exec grep 'search string' /dev/null '{}' \+ </syntaxhighlight> The <code>[[/dev/null]]</code> argument is used to show the name of the file before the text that is found. Without it, only the text found is printed. (Alternatively, some versions of grep support a {{mono|-H}} flag that forces the file name to be printed.) GNU <code>grep</code> can be used on its own to perform this task: <syntaxhighlight lang="console"> $ grep -r 'search string' /tmp </syntaxhighlight> Example of search for "LOG" in jsmith's home directory tree: <syntaxhighlight lang="console"> $ 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 </syntaxhighlight> Example of search for the string "ERROR" in all XML files in the current working directory tree: <syntaxhighlight lang="console"> $ find . -name "*.xml" -exec grep "ERROR" /dev/null '{}' \+ </syntaxhighlight> 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: <syntaxhighlight lang="console"> $ find . -name "file-containing-can't" -exec grep "can't" '{}' \; -print </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)