9.21. grepping a Directory TreeWant 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 commands. 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 Using the backquotes (``) might not work. If find finds too many files, egrep's command-line arguments can get too long. Using xargs 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 won't print the filename: % 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," /dev/null. 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 Then xargs will run commands like these: 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 (Section 28.3) and only one file might match it. grep won't always print the file's name unless you add /dev/null: % grep "whatever" /dev/null /x/y/z/a* -- JP Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|