From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <96078701111612.tru64-unix-managers.marc@archive.invalid> From: Mark Schlam Date: Mon, 23 Mar 1998 14:59:55 +0000 To: tru64-unix-managers Subject: SUMMARY: grep - an easy question X-MARC-Message: https://marc.info/?l=tru64-unix-managers&m=96078701111612 The consensus: find /foo -type f -exec grep "" {} \; Many thanks to everyone who replied! Original question: | Most of us know that to search for a string in a given file | grep "" file works and | to search for a string in all files in an entire directory | grep "" * works | but suppose you have a whole set of directories you wish to search through where you search for the string in multiple directories | /- | /foo | /foo1 /foo2 /foo3 /foo4 | file1 file2 file3 file4 | Other alternative responses: find . -type f | xargs grep string /dev/null grep "" /foo/*/* .//. cd /foo find . | xargs grep 'string' find /foo -exec grep "" {} /dev/null \; find / -name "file*" -print -exec grep "string" {} \; find . -type f | xargs grep "" find dir1 dir2 dir3 -type f -exec grep "string" {} \; grep "find me" */*/filemask* */*/*/filemask* # grep /foo/foo[1-4]/file[1-4] # grep /foo/{foo1,foo2,bar1,bar2}/file[1-4] find -name -print -exec grep {} \; find dir -exec grep "" {] \; -print The 'dir' is the directory where you want to start searching from. '-print' will print out the path/file the string was found in. #!/bin/csh -f foreach i ( `find .` ) if ( -f $i ) grep -i string $i end or find -type f -exec grep -l {} \; find / -name '*.*' -print -exec grep "" {} \; remove the " -name '*.*' " part of the above to get every file..... #!/bin/sh # name of the shell script is 'grepa' if test $# -eq 0 then echo "Usage: grepa " echo "Usage: grepa " exit elif test $# -eq 1 then echo "find . -type f -print | xargs fgrep $1 /dev/null" find . -type f -print | xargs fgrep $1 /dev/null exit elif test $# -eq 2 then echo "find $1 -type f -print | xargs fgrep $2 /dev/null" find $1 -type f -print | xargs fgrep $2 /dev/null exit else echo "too many arguments" echo "Usage: grepa " echo "Usage: grepa " exit fi find -type f -print | xargs fgrep /dev/null find looks for all files starting at a given directory und pipes it to fgrep which search then for the string in these files. find . -type f -print |xargs grep "string" grep "string" */* find / -name file\* -exec grep string {} \; this will find all files named file* beginning in / and then run grep for string on each file. Or if you didn't want to search all subdirectories, you could use a korn shell script. #!/bin/ksh dirlist="dir1 dir2 dir3 dir4" for dir in dirlist do find $dir/ -name file\* -exec grep string {} \; ----or--- grep string filename done