Need to search strings in (.gz) zipped folder with files in it.

Sample Folder -


Files inside - PROD_009_010919.log01, PROD_009_010919.log02, PROD_009_010919.log03.......etc

Command - zgrep -ia *123456* PROD_* is not fetching the expected results.

the expected output - i should search the string '123456' in all the files in the zipped folders and display the string containing files.

closed as unclear what you're asking by Kusalananda, Mr Shunz, Jeff Schaller, peterh, Thomas Jan 12 at 18:12

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • What do you want to do? Do you want to extract a piece of text from one of the files in the archive, or do you want to test whether a particular file is part of the archive? In short, what is the expected result? – Kusalananda Jan 11 at 12:30
  • gzip does not zip folders, only files so I suppose that you are using another zipper apart from gz right? Have you checked with gzip -l the content of of your gz? – Dasel Jan 11 at 12:37
  • @Dasel, based on the filename it seems to be tar-ed before gzip-ed – Romeo Ninov Jan 11 at 12:43
  • 1
    Thanks @RomeoNinov; I did not saw the name; in that case we need first to know which is expected, as otherwise is difficult to debug. Anyway it would be much easier to do tar -ztvf PROD_009_010919_0110.tar.gz and after that use a normal grep to search the wanted result, but of course the ideal would be to have a sample of the expected output. – Dasel Jan 11 at 12:54
  • Thanks kusal, Dasel, Romeo. – Praveen K P Jan 11 at 13:28

This prints out the list of files inside the tarball matching the given pattern:

tar --ignore-command-error -xvf PROD_009_010919_0110.tar.gz --to-command="grep -FH 1234536 -" | grep -B1 --no-group-separator '(standard input)' | grep -v '(standard input)'

The --to-command option extracts each file and sends to the standard input of the grep command. The -v option lists each file as they're processed.

The --ignore-command-error is used to ignore the exit status when grep cannot find a match. Because of the -H option (print filename) used with the grep command, each matching line is prefixed with a '(standard input)' string.

This results in output of the following kind from the tar command:

(standard input): <matched lines from file2>
(standard input): <matched lines from file3>

Piping this output allows the two grep commands to extract only the file names which are immediately followed by the '(standard input)' string on the next line. This processing could probably be improved using a regex to match the pattern instead of the two sequential grep commands I have used here.

The resulting output in this case will be:

  • We still don't actually know whether the user wants to grep filenames or file contents. – Kusalananda Jan 11 at 15:01
  • File content Kusal. – Praveen K P Jan 11 at 15:30
  • @Kusalananda I was going by the OP's earlier comment that stated 'i should get the string 1234536 containing list of files'. In any case, the OP has just clarified with another comment that they want to grep through the contents and then list the files containing matches. – Haxiel Jan 11 at 15:53
  • @PraveenKP To reply to a specific user, you should use the '@username' notation so that they get a notification for your comment. In any case, I believe my answer should work as per your requirements. Let me know if you see any problems. – Haxiel Jan 11 at 15:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.