Ubuntu – Read specific content file from tar.gz files without extracting

command linegrep

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?

Best Answer

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.

Related Question