StackJay
StackJay

Reputation: 61

How do I search files containing a specific string using grep?

For instance, if a file has a line "blahblah myID=1234567 blahblah", I want to search all files containing 1234567 somewhere in the whole file.

I tried grep -r '.* 1234567.* ' directory, but it didn't work.

Upvotes: 0

Views: 1247

Answers (2)

eLemEnt
eLemEnt

Reputation: 1801

Do the following:

grep -rw 'directory' -e "pattern"

-r is recursive and -w stands match the whole word.

example

grep -rw '/home/lib/foldername/' -e "1234567"

you can also use -n which will tell you the line number where it matched the string

Upvotes: 3

michael_stackof
michael_stackof

Reputation: 223

files=$(ls -l /dir |awk '/^-/ {print $NF}')
for i in $files
do
      cat $i | grep "1234567" >> output.txt
done   

List files of /dir, and grep "1234567" and write to output.txt

Upvotes: 0

Related Questions