Want to search every file, in some directory and all its subdirectories,
to find the file that has a particular word or string in it?
That's a job for
and one of the
For example, to search all the files for lines starting with a number
and containing the words "SALE PRICE," you could use:
egrep '^[0-9].*SALE PRICE' `find . -type f -print`
./archive/ad.1290: 1.99 a special SALE PRICE
./archive/ad.0191: 2.49 a special SALE PRICE
might not work.
finds too many files, egrep
command-line arguments can
get too long (9.20
can solve that; it splits long sets of arguments into smaller chunks.
There's a problem with that: if the last "chunk" has just one filename
and the grep
command finds a match there, grep
find . -type f -print | xargs fgrep '$12.99'
./old_sales/ad.0489: Get it for only $12.99!
./old_sales/ad.0589: Last chance at $12.99, this month!
Get it for only $12.99 today.
The answer is to add the UNIX "empty file,"
It's a filename that's guaranteed never to match but
always to leave fgrep
with at least two filenames:
find . -type f -print | xargs fgrep '$12.99' /dev/null
will run commands like:
fgrep '$12.99' /dev/null ./afile ./bfile ...
fgrep '$12.99' /dev/null ./archives/ad.0190 ./archives/ad.0290 ...
fgrep '$12.99' /dev/null ./old_sales/ad.1289
That trick is also good when you use a wildcard and only one file might
won't always print the file's name unless you add /dev/null
" /dev/null /x/y/z/a*