How to grep all the files inside the directory and Sub directory

Hi,

I have used the command

cat * | grep -r <<String>> *

It returns:

cat : JAN : is directory

***********************
*********************

My directory structure:

     log
       - JAN
          -catalina.out
       -FEB
          -catalina.out
      -MARCH
          -catalina.out

Please Suggest me

Regards,
Nanthagopal

Hi , Try the below command.

find . -type f -exec grep <string> /dev/null {} \;

Dont you have -R option in Grep?

Hi,

Try this command this will list the file containing the string/pattern.

grep -lr <string/pattern>  directory path

Thanks for your reply,

@Jairaj,

  It's working fine. but using the above command, how to grep the string in this format 
grep <string> | grep <string>

By grep string1 file| grep string2 , I assume that you require to search and print those lines which contain both string1 and string2. This could be done like this:

grep -E 'string1.*string2|string2.*string1' file

Use below code to search through all the files from the directory ...

#Single Directory --
grep -r "text" /path/*

#Multiple Directory --

grep -r "text" /path/* /path2/* 

If you want to search multiple strings in a particular folder use the below commands.

grep -r '<pattern1>\|<pattern2>\|etc' /path/to/dirctory

the above command will yield the output of line containing the pattern in all files in that particular directory.

If you want to list the files alone which has the pattern use the command given below

grep -lr  '<pattern1>\|<pattern2>\|etc' /path/to/dirctory

This will print/select only those lines which contain either <pattern1> or <pattern2> or .... That's not the same as <pattern1> and <pattern2> and ... which the OP seemingly desires.

Hi elixir_sinari,

Have tried using that command. When i attempted it resulted me with both the patterns.I think you have misunderstood the symbol '|' :confused:

Check this:

$ cat testfile
WORD1 WORD2
ONLY WORD1
ONLY WORD2

$ grep 'WORD1\|WORD2' testfile
WORD1 WORD2
ONLY WORD1
ONLY WORD2

Now, don't you wonder why I am getting all three lines when I require only the first line which has both WORD1 and WORD2?

With slight modifications:

$ grep 'WORD1.*WORD2\|WORD2.*WORD1' testfile
WORD1 WORD2

Do you still think I am mistaken?

1 Like

sorry dude... now its clear... actually i was little bit confused