LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   read a file from compresses tar directory (https://www.linuxquestions.org/questions/linux-newbie-8/read-a-file-from-compresses-tar-directory-4175609199/)

Ankit yadav 07-05-2017 03:54 AM

read a file from compresses tar directory
 
Is there is any way to read the contents of a file that is in compressed tar.

If I run tar -tf compressed.tar.gz

I can see that file listed there let say file1.

How to cat(read) that file without extract it from tar. like we can see compressed files using zcat and bzcat.

Turbocapitalist 07-05-2017 05:33 AM

Does your version of tar support the -O or --to-stdout option?

Code:

tar --to-stdout -zxf compressed.tar.gz somepath/somefile.txt | less

aragorn2101 07-05-2017 05:40 AM

Let's say you have an archive called "compressed.tar.gz" and that the archive contains a directory named compressed under which we have:
Code:

compressed
 - file1
 - file2
 - file3
 - file4

Then you can do
Code:

cat $( tar xvf compressed.tar.gz compressed/file1 )
The trick is that the tar command will extract file1 from the archive and put it under directory compressed/. Then it gives cat the path, so you have the output in stdout. The "-t" option only lists the file inside instead of extracting that one file; this is why you could not read it.

hazel 07-05-2017 05:50 AM

This is the kind of problem where reading the relevant man (manual) page is often a quick solution. As a newbie, you need to learn to use man pages as a first port of call. They are extremely useful and informative. Your second port of call is google or similar. Then come and ask us.

man tar will list the man page for the tar command. You will see that the -x option allows you to extract files. By itself, it extracts the whole content, but if you add the internal pathname of the file you want to look at (obtained by using -t), it will extract just that one. The -O option will list the file at the terminal, otherwise it will put the extracted file into the current directory for you to read later.

Ankit yadav 07-05-2017 08:38 AM

Thanks for replies. I did read man page but was not able to understand how to do it. I got it now thanks. Will do more research before posting any question. :)

rtmistler 07-05-2017 08:56 AM

Quote:

Originally Posted by Ankit yadav (Post 5730741)
Is there is any way to read the contents of a file that is in compressed tar.

If I run tar -tf compressed.tar.gz

I can see that file listed there let say file1.

How to cat(read) that file without extract it from tar. like we can see compressed files using zcat and bzcat.

Quote:

Originally Posted by Ankit yadav (Post 5730820)
Thanks for replies. I did read man page but was not able to understand how to do it. I got it now thanks. Will do more research before posting any question. :)

Your original question did illustrate that you've tried some things, and as you've said you did read the manual page but didn't fully grasp what options you had. Therefore do not feel poorly about your question, I think it was a fine one, and some things you can take away from Hazel's advice is a good process you can follow. Meanwhile, we all get stuck and it's good to know that you got some helpful answers here.

szboardstretcher 07-05-2017 10:06 AM

Fun copy/paste python solution...
  • Create tar gzip with contents
  • Create python script
  • Run python script

Code:

mkdir test; cd test
for i in {1..5}; do echo "TESTING TESTING $i" > MyCamelCaseFile$i; done
tar czf my.tgz MyCamelCaseFile*

cat << EOF > pytarlooker.py
#!/bin/python
import tarfile
with tarfile.open("my.tgz","r:gz") as tar:
 print tar.extractfile("MyCamelCaseFile3").read()
EOF

python pytarlooker.py


Shadow_7 07-05-2017 10:35 AM

You can extract a single file by using the same line to extract the whole with the filename (as it appears in the tar file) as an extra parameter.

$ tar -xzvpf mytarball.tar.gz ./some/path/to/a/file/file.txt

Which results in just that file being extracted (plus the path portion too).

$ man tar

$ tar --help | less

All of which has already been said in the thread in one form or another.

szboardstretcher 07-05-2017 10:51 AM

Quote:

Originally Posted by Shadow_7 (Post 5730888)
You can extract a single file by using the same line to extract the whole with the filename (as it appears in the tar file) as an extra parameter.

I think you missed this bit:

Quote:

How to cat(read) that file without extract it from tar.


All times are GMT -5. The time now is 09:25 PM.