Differences between revisions 11 and 14 (spanning 3 versions)
Revision 11 as of 2010-06-28 19:23:18
Size: 1608
Editor: GreyCat
Comment:
Revision 14 as of 2015-03-05 00:24:46
Size: 1828
Editor: izabera
Comment: syntax hl
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
90% of the time, all you need is one of these: If you are on a typical GNU or BSD system, all you need is one of these:
Line 5: Line 5:
{{{ {{{#!highlight bash
Line 7: Line 7:
grep -r "$search" . grep -r -- "$search" .
Line 10: Line 10:
grep -r -l "$search" . grep -r -l -- "$search" .
Line 12: Line 12:
You can use [[UsingFind|find]] if your {{{grep}}} lacks a {{{-r}}} option:
Line 14: Line 13:
{{{
find . -type f -exec grep -l "$search" {} \;
If your `grep` lacks a `-r` option, you can [[UsingFind|use find]] to do the recursion:

{{{#!highlight bash
# Portable but slow
find . -type f -exec grep -l -- "$search" {} \;
Line 17: Line 19:
The {} characters will be replaced with the current file name.
Line 21: Line 22:
{{{
find . -type f -exec grep -l "$search" {} +
{{{#!highlight bash
# Fast, but requires a recent find

find . -type f -exec grep -l -- "$search" {} +
Line 24: Line 26:
The trailing '+' character instructs {{{find}}} to call {{{grep}}} with as many file names as possible, saving processes and resulting in faster execution. This example works for POSIX {{{find}}}, e.g. with Solaris, as well as ''very'' recent GNU {{{find}}}. The trailing '+' character instructs {{{find}}} to call {{{grep}}} with as many file names as possible, saving processes and resulting in faster execution. This example works for POSIX-2008 {{{find}}}, which ''most'' current operating systems have, but which may not be available on legacy systems.
Line 28: Line 30:
{{{
find . -type f | xargs grep -l "$search"
{{{#!highlight bash
# DO NOT USE THIS

find . -type f | xargs grep -l -- "$search"
Line 31: Line 34:
However, if your filenames contain spaces or other metacharacters, you'll need to use the BSD/GNU {{{-print0}}} option: However, if your filenames contain spaces, quotes or other metacharacters, this will fail catastrophically. BSD/GNU `xargs` has a `-print0` option:
Line 33: Line 36:
{{{
find . -type f -print0 | xargs -0 grep -l "$search"
{{{#!highlight bash
find . -type f -print0 | xargs -0 grep -l -- "$search"

How can I recursively search all files for a string?

If you are on a typical GNU or BSD system, all you need is one of these:

   1 # Recurse and print matching lines (GNU grep):
   2 grep -r -- "$search" .
   3 
   4 # Recurse and print only the filenames (GNU grep):
   5 grep -r -l -- "$search" .

If your grep lacks a -r option, you can use find to do the recursion:

   1 # Portable but slow
   2 find . -type f -exec grep -l -- "$search" {} \;

This command is slower than it needs to be, because find will call grep with only one file name, resulting in many grep invocations (one per file). Since grep accepts multiple file names on the command line, find can be instructed to call it with several file names at once:

   1 # Fast, but requires a recent find
   2 find . -type f -exec grep -l -- "$search" {} +

The trailing '+' character instructs find to call grep with as many file names as possible, saving processes and resulting in faster execution. This example works for POSIX-2008 find, which most current operating systems have, but which may not be available on legacy systems.

Traditional Unix has a helper program called xargs for the same purpose:

   1 # DO NOT USE THIS
   2 find . -type f | xargs grep -l -- "$search"

However, if your filenames contain spaces, quotes or other metacharacters, this will fail catastrophically. BSD/GNU xargs has a -print0 option:

   1 find . -type f -print0 | xargs -0 grep -l -- "$search"

The -print0 / -0 options ensure that any file name can be processed, even one containing blanks, TAB characters, or newlines.


CategoryShell

BashFAQ/008 (last edited 2015-03-05 00:24:46 by izabera)