I wish to write the result of these below conditions in a file:

  1. in a specific folder, I have many tar.gz files.
  2. each tar.gz file contains .dat file in sub folders.
  3. I wish to get the full path of these .dat files, if I find in it a specific word (ERROR24).
  4. all this without extracting the tar.gz files

I have found and tested the below one that writes in a file the result, but I don't how to look in a tar.gz file.

grep -rlw --include="*.dat" -e "ERROR24" /home/tests/logs > /home/files/data/result/listErrors.txt

Can you please help update the previous one in order to get the same result but searching in tar.gz files?

  • 1
    Use zgrep instead on *.tar.gz files – αғsнιη Nov 1 '17 at 13:58
  • thanks for your advice. (i can't test right know... i don't have my linux laptop... tomorrow i'll be able to). i'll test with zgrep -rlw --include="*.dat" -e "ERROR24" /home/tests/logs > /home/files/data/result/listErrors.txt – JamesJimmy Nov 1 '17 at 19:12
  • hello it is not working... i am getting this : "/usr/bin/zgrep: -rlw: option not supported" do you another solution please? – JamesJimmy Nov 2 '17 at 8:04

As mentioned in the comments, you need to use zgrep on compressed archives, however what you're doing is adding -r to the options list, which is not supported by zgrep which comes with Ubuntu.

-r is used for recursive traversal of directory tree. We can do the same with find command, and use -exec flag to run zgrep on each compressed archive that is found.

Since I don't have any example of archives you use, the command below is just an example. Adjust as necessary:

find -type f -name "*.tar.gz" -exec zgrep -a "ERROR24" /home/tests/logs > /home/files/data/result/listErrors.txt  \;

Note that -l here can't really be used, because it only lists filename of archive itself. Alternatively, you could always use zcat and filter its output with grep, but that's probably more hassle than necessary.

  • thanks for you help. (i am a newbie). i have tried your advice and i am getting "-exec: command not found" – JamesJimmy Nov 6 '17 at 8:06
  • if i use the pipe | instead of -exec, i am not getting the error but i have no result. meaning it is not working. – JamesJimmy Nov 6 '17 at 10:20
  • @JamesJimmy Sorry, exec should be terminated with \;. I'll update my answer – Sergiy Kolodyazhnyy Nov 6 '17 at 19:08

IF you mean just viewing what's within the archive you can simply change what it opens in or does from the right-click menu whenever you click on the archive. As in resetting the default application/action.

I know for a lot of archives the default action now is to mount it... for whatever reason or to automatically extract.

Or are you looking for a command-line oriented solution?

  • command-line oriented solution = yes i am. there are too many archive, impossible to do it manually. so please advise how i can do it? thanks – JamesJimmy Nov 1 '17 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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