Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a tar.gz which have pdfs in it I want to open a specific pdf without extracting it Is this possible using terminal through some command (I know we can do it using GUI but I want to use terminal)

share|improve this question
1  
GUI apps also stores as temporary files at $HOME/.cache/ –  Pandya May 17 at 9:04

3 Answers 3

up vote 4 down vote accepted

To list (not-open) files in archive without extracting, run following command:

tar -tf myarchive.tar.gz

To extract and open directly run following command:

tar -xzf myarchive.tar.gz && evince *.pdf

For quick process: (extacted in /tmp/mypdf and then removed)

mkdir /tmp/mypdf && tar -xzf myarchive.tar.gz -C /tmp/mypdf && cd /tmp/mypdf && evince *.pdf && cd .. && rm -r mypdf

Where myarchive.tar.gz is your sample archive name.


For extracting specific file,

mkdir /tmp/mypdf && tar -xzf myarchive.tar.gz -C /tmp/mypdf "<file.pdf>" && cd /tmp/mypdf && evince *.pdf && cd .. && rm -r mypdf

where "<file.pdf>" is file-name of that specific file you want to extract & open.

Hope it is helpful. For advance function scripted command is useful by running script for specific purpose.

share|improve this answer
    
For quick create and delete is added last. –  Pandya May 17 at 10:56
    
Thanks Sir It worked as I wanted Can you explain what does -C mean? –  Gaurav Dalpatyan May 18 at 0:09
    
your solution is extracting all files isn't there a way that it extracts only the file which I want to view I tried adding file name after tar file name but It was not getting extracted –  Gaurav Dalpatyan May 18 at 0:25
    
@GauravDalpatyan : I added last option for only specific file where <file.pdf> is name of file which you want to open. –  Pandya May 18 at 7:52
    
From tar --help ->-x, --extract, --get extract files from an archive. Op wants to open a file in tar.gz archive without extracting it. –  Avinash Raj May 18 at 7:52

To do this in a single command line, you need a pdf viewer which can handle input from a pipe. The only viewer with this option I know is ImageMagicks's display. It is not very comfortable (larger files may take a while), but may be it is convenient for your purpose.

tar xOfz <archive> <path/to/file/in/archive> | display

(Larger pdf files may take a while.)

share|improve this answer
    
Sorry tried installing imagemagick but returned with error code 1 Well I think it might work –  Gaurav Dalpatyan May 18 at 0:27
    
Finally able to install but it hanged after loading a page –  Gaurav Dalpatyan May 18 at 2:23
    
From tar --help ->-x, --extract, --get extract files from an archive. Op wants to open a file in tar.gz archive without extracting it. –  Avinash Raj May 18 at 7:53
    
@AvinashRaj There is no way to process a file without extraction from its archive. In my case no file needs to be written (see Golboth's comment). –  TAq May 18 at 19:19
    
Why you failed to mention this in your answer? –  Avinash Raj May 18 at 22:08

Opening without extracting is not possible. GUI applications are extracting the files temporarily before opening them...

If you have one tar.gz file containing PDF files, and you wish to extract one or more files using command line, you can use:

tar -xzf MyArchive.tar.gz MyPDF5.pdf

tar -xzf MyArchive.tar.gz MyPDF6.pdf MyPDF3.pdf

tar -xzf MyArchive.tar.gz "My PDF 128.pdf"

You can also extract one file from a folder in the archive (the directory structure will be kept):

tar -xzf MyArchive.tar.gz "My Archive Folder/My Other PDF.pdf"

If you don't wish to keep the directory structure, you can use --strip-components followed by the level of the subfolder:

tar -xzf MyArchive.tar.gz "My Archive Folder/My Other PDF.pdf" --strip-components 1

tar -xzf MyArchive.tar.gz "e-Books/Favorites/Ubuntu Linux/My PDF about Ubuntu.pdf" --strip-components 3

Please note that your archive file name must also be double-quoted if it contains spaces.

In the examples above, I'm supposing that you are in the folder containing the archive file and that you wish to extract there.

Before using tar command, you should use "cd" command to go to the folder in which you wish to extract the files. If the archive is located in another folder, simply put its absolute full path ["/home/myusername/Desktop/My Folder/My Other Archive.tar.gz"] (using a relative path may also be useful in some cases).

P.S.: If it was a simple text file (txt), "-O" parameter at the end of the command might have been what you were searching for, but since it is a PDF one, it is not very useful... According to TAq answer, "display" from ImageMagick is able to handle that PDF output the way you wish with a pipe.

tar -xzf MyArchive.tar.gz "My PDF 128.pdf" -O | display
share|improve this answer
    
From tar --help ->-x, --extract, --get extract files from an archive. Op wants to open a file in tar.gz archive without extracting it. –  Avinash Raj May 18 at 7:53
    
Yes, you're right. That's why I stated that "Opening without extracting is not possible" and that "GUI applications are extracting the files temporarily before opening them". Even with the parameter "-O" which allows to extract to the standard output, tar is still extracting, but not as a file on disk, which is somewhat closer to the initial goal of Gaurav Dalpatyan. –  Golboth May 18 at 9:20
    
I didn't see that so +1. –  Avinash Raj May 18 at 9:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.