LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-05-2017, 03:54 AM   #1
Ankit yadav
LQ Newbie
 
Registered: Jun 2017
Posts: 22

Rep: Reputation: Disabled
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.
 
Old 07-05-2017, 05:33 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003Reputation: 2003
Does your version of tar support the -O or --to-stdout option?

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

Last edited by Turbocapitalist; 07-05-2017 at 05:49 AM. Reason: t -> x
 
2 members found this post helpful.
Old 07-05-2017, 05:40 AM   #3
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 546

Rep: Reputation: 270Reputation: 270Reputation: 270
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.
 
1 members found this post helpful.
Old 07-05-2017, 05:50 AM   #4
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 3,289
Blog Entries: 9

Rep: Reputation: 1840Reputation: 1840Reputation: 1840Reputation: 1840Reputation: 1840Reputation: 1840Reputation: 1840Reputation: 1840Reputation: 1840Reputation: 1840Reputation: 1840
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.
 
1 members found this post helpful.
Old 07-05-2017, 08:38 AM   #5
Ankit yadav
LQ Newbie
 
Registered: Jun 2017
Posts: 22

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-05-2017, 08:56 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,013
Blog Entries: 13

Rep: Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460Reputation: 3460
Quote:
Originally Posted by Ankit yadav View Post
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 View Post
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.
 
Old 07-05-2017, 10:06 AM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651
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
 
Old 07-05-2017, 10:35 AM   #8
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 3,911
Blog Entries: 1

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
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.
 
Old 07-05-2017, 10:51 AM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651
Quote:
Originally Posted by Shadow_7 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tar - file changed as we read it robertkwild CentOS 23 03-14-2017 03:31 AM
Script Issue with Tar Command: Either tar file created in wrong directory or not at all JockVSJock Linux - Newbie 4 06-09-2016 10:18 PM
Script to find tar archives, read tar file contents, output content to an index file. bluesword1969 Linux - General 4 02-07-2011 12:15 PM
MondoArchive compresses 4.7GB directory to 40GB? brianpbarnes Linux - Software 0 01-23-2009 11:30 AM
'Read-only file system' when untar a tar.bz2 file yinglcs Linux - General 1 02-18-2006 09:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration