Using the egrep
program, we can recursively search a directory for a certain pattern. Here is a command line that is used to search a directory recursively for text files that contain a pattern:
~> find ./data -name '*.txt' -type f -print | xargs egrep 'this' /dev/null ./data/a/a.txt:this is ./data/a/b/b.txt:this is file ./data/a/b/c/c.txt:this is file
Before we start grokking the command line, let's see what the xargs
argument does. Xargs
takes the egrep
command, in this case, and invokes it with the arguments that it reads from the standard input.
For example, the following command searches for the pattern with help of the argument:
~> echo 'data.txt' | xargs egrep 'this'
The xargs
argument invokes egrep
on the data.txt
file and searches for the this
pattern. Xargs
packs in as many arguments as possible (there is a limit) to minimize the number of egrep
invocations. Refer to 'man xargs' for more information.
Going back to our command, which has two parts...