I wish to write the result of these below conditions in a file:
- in a specific folder, I have many
tar.gz
files. - each
tar.gz
file contains.dat
file in sub folders. - I wish to get the full path of these
.dat
files, if I find in it a specific word (ERROR24
). - 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 byzgrep
which comes with Ubuntu.-r
is used for recursive traversal of directory tree. We can do the same withfind
command, and use-exec
flag to runzgrep
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:
Note that
-l
here can't really be used, because it only lists filename of archive itself. Alternatively, you could always usezcat
and filter its output withgrep
, but that's probably more hassle than necessary.