Sudhakar
Sudhakar

Reputation: 97

Unix script to search for a specific keyword in all files and sub directories of a directories or folder

I am working on a shell script that will search for a specific keyword in all log files and subfolders in a folder.

I am using below grep command but it is displaying the keyword only from the current directory but not from subdirectories. in my case, I have bunch of subdirectories where the log files will be there where it is not displaying the files from subdirectory.

grep -r -H "Keyword" *.log

please suggest how can I achieve this.

Thanks

Upvotes: 2

Views: 1789

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133508

Could you please try following.

find /dir_name/ -type f -name "*.log" -exec grep -l "your_string" {} \+ 2>/dev/null

Upvotes: 0

Bayou
Bayou

Reputation: 3441

You're using the wrong parameter and the wrong command. With grep, it should be -R and you can only search in directories.

grep -R -H "Keyword" .

But I think you want to use find.

find . -name '*.log' -exec grep -H "Keyword" {} \;

Upvotes: 1

Related Questions