Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to see the content of a particular file in .tar.gz archive without unzipping the contents?

Tags:

python

bash

for ex abc.tar.gz has

abc/file1.txt
abc/file2.txt
abc/abc1/file3.txt
abc/abc2/file4.txt

i need to read/display the contents of file3.txt without extracting the file.

Thanks for any input.

like image 809
szhak Avatar asked Jul 30 '10 08:07

szhak


People also ask

How can I view the contents of a tar gz file without extracting it?

Use -t switch with tar command to list content of a archive. tar file without actually extracting. You can see that output is pretty similar to the result of ls -l command.

How do I open a gz file without unzipping it in Linux?

If you need to check the contents of a compressed text file on Linux, you don't have to uncompress it first. Instead, you can use a zcat or bzcat command to extract and display file contents while leaving the file intact. The "cat" in each command name tells you that the command's purpose is to display content.


1 Answers

import tarfile
spam = tarfile.open( "abc.tar.gz" )
if "abc/abc1/file3.txt" in spam.getnames():
    with spam.extractfile( "abc/abc1/file3.txt" ) as ham:
        print ham.read()

See tarfile.

like image 176
Katriel Avatar answered Oct 06 '22 00:10

Katriel