• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 88
  • Last Modified:

grep a tar file

grepping a xyz.tar.gz file?

how to grep a xyz.tar.gz file on unix box?

i moved it to loical using winscp.

how to extract it to see file contents

i used qizip and 7 zip they simply changing zip to tar and then tar to zip again and again

please advise
1 Solution
Dr. KlahnPrincipal Software EngineerCommented:
grepping a xyz.tar.gz file?

The file must be decompressed in order to turn it back into a TAR file, e.g. for a gzipped file, gzip -d <xyz.tar.gz>

The TAR file should then be unTARed to extract the contents, e.g., tar -xvf <xyz.tar>.  It's possible to grep the TAR file itself, but if the TAR file contains multiple files and there is a match - there's no way to tell which of the files in the archive contains the target string.

Depending on the version of tar available, there may be a direct switch to extract from a compressed tar file; use "man tar" to find out.

Finally grep the unTARed contents, e.g. grep <regex> *
nociSoftware EngineerCommented:
tar.gz can be read in one go:
(x = extract, z = gunzip, v = show what you do, f next argument in input file)
tar xzvf {xyz.tar.gz}      # don;t use { }...  

(t = test, z = gunzip, v = verbose, f = next is file) - testing will show the contents of a tar.
tar tzvf {xyz.tar.gz}    

What do you want to know..., if some word occurs in the tar as such? then grepping .tar file makes sense
zcat {xyz.tar.gz} | grep the-word

if you want some specific file checked..., then you need to unpack.
gudii9Author Commented:
tar xzvf xyz.tar.gz

showing bunch of files there like


what are all those files

how do i grep on ./xyxstuff-2018-02-24 which is more important to me?
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Travis MartinezSmoke JumperCommented:
The above comments are probably what you're looking for; however, if you would like to unpack and/or change the extension try using WinRAR.  I'm fairly confident though you have to unpack it first with WinRAR before you can change the extension of the file.
Dr. KlahnPrincipal Software EngineerCommented:
how do i grep on ./xyxstuff-2018-02-24 which is more important to me?

See my comment above, and instead of grepping on *, grep on the filename.
Tar is an archive, gz means it is compressed similar to placed in a box and sealed.

Winrar if not mistaken includes untar functionality.

As others pointed out you gave to unseal, unpack box and only then look through to files ...

While Linux/UNIX is command line oriented, grep get look at the data stream, what is the data that you want

unless you do two things, 1 prior to archiving, catalog, index the files such that you have the archive, file.tar.gz and the other index_catalog.gz
As a hash, cdb, etc. then when searching if pattern is in file.tar.gz, you would search for the pattern in the index as that would be smaller space......
That will tell if there are matches and when they exists the files in which they San ve located.
nociSoftware EngineerCommented:
grep someword ./xyxstuff-2018-02-24

maybe using view/notepad/wordpad/ultraedit/... show more in the case of ascii data.
A tar file is just basically a concatenation of files with some dividers in it.  A tar.gz is a tar file that's been compressed.  If the tar'd portion is plain text, you can use zgrep, if you have zcommands installed on your system.

zgrep <search_term> xyz.tar.gz

Seth SimmonsSr. Systems AdministratorCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Dr. Klahn (https:#a42481531)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Cleanup Volunteer
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now