2

I want to find all the occurrences of "getId" inside a directory which has subdirectories as follows:

*/*/*/*/*/*/myfile.gz

i tried thisfind -name *myfile.gz -print0 | xargs -0 zgrep -i "getId" but it didn't work. Can anyone tell me the best and simplest approach to get this?

  • what error did you get? and Also you need to add the directory as well. try:: find . -name "myFile.gz" -type f -exec zgrep -i getSorById {} \; also in some , you may have to use gzgrep – abhishek phukan Oct 10 '17 at 08:53
  • if you would like to print only the files which have that keyword use grep -il . also if that is what you require you can use grep -ril "keyword" * – abhishek phukan Oct 10 '17 at 08:58
  • @abhishekphukan it is not giving the expected result although it is not throwing any error. –  Oct 10 '17 at 09:25
  • Could you please share the expected output – abhishek phukan Oct 10 '17 at 09:38

2 Answers2

2
find ./ -name '*gz' -exec zgrep -aiH 'getSorById' {} \;

find allows you to execute a command on the file using "-exe" and it replaces "{}" with the file name, you terminate the command with "\;"

I added "-H" to zgrep so it also prints out the file path when it has a match, as its helpful. "-a" treats binary files as text (since you might get tar-ed gzipped files)

Lastly, its best to quote your strings in case bash starts globbing them.

https://linux.die.net/man/1/grep https://linux.die.net/man/1/find

MostWanted
  • 571
  • 1
  • 5
  • 12
0

Use the following find approach:

find . -name *myfile.gz -exec zgrep -ai 'getSORByID' {} \;

This will print all possible lines containing getSORByID substring

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105