17.20 grepping a Directory Tree (and a Gotcha)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 find and one of the grep ( 27.1 ) commands. For example, to search all the files for lines starting with a number and containing the words "SALE PRICE," you could use:
%
Using the
backquotes (
9.16
)
(
The answer is to add the UNIX "empty file," /dev/null ( 13.14 ) . It's a filename that's guaranteed never to match but always to leave fgrep with at least two filenames:
% Then xargs 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 match it. grep won't always print the file's name unless you add /dev/null :
% - |
|