SUMMARY: grep - an easy question

From: Mark Schlam <mslist_at_castle.beaver.edu>
Date: Mon, 23 Mar 1998 09:59:55 -0500 (EST)

The consensus:

        find /foo -type f -exec grep "<string>" {} \;

Many thanks to everyone who replied!

Original question:
| Most of us know that to search for a string in a given file
| grep "<string>" file works and
| to search for a string in all files in an entire directory
| grep "<string>" * 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
| <string> <string> <string> <string>

Other alternative responses:
find . -type f | xargs grep string /dev/null

grep "<string>" /foo/*/*
.//.


cd /foo
find . | xargs grep 'string'

find /foo -exec grep "<string>" {} /dev/null \;

find / -name "file*" -print -exec grep "string" {} \;

find . -type f | xargs grep "<string>"

find dir1 dir2 dir3 -type f -exec grep "string" {} \;

grep "find me" */*/filemask* */*/*/filemask*

# grep <string> /foo/foo[1-4]/file[1-4]
# grep <string> /foo/{foo1,foo2,bar1,bar2}/file[1-4]

find <start-directory> -name <file-pattern>
                              -print -exec grep <string> {} \;

find dir -exec grep "<string>" {] \; -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 <startdir> -type f -exec grep -l <string> {} \;

find / -name '*.*' -print -exec grep "<string>" {} \;
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 <search directory> <search string>"
     echo "Usage: grepa <search string>"
     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 <search directory> <search string>"
    echo "Usage: grepa <search string>"
    exit
fi

find <start-directory> -type f -print | xargs fgrep <search string>
/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
Received on Mon Mar 23 1998 - 15:57:21 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:37 NZDT