Search code examples
tarxz

extract a file from xz file


I have a huge file file.tar.xz containing many smaller text files with a similar structure. I want to quickly examine a file out of the compressed file and have a glimpse of files content structure. I don't have information about names of the files within the compressed file. Is there anyway to extract a single file out given the above the above scenario?

Thank you.

EDIT: I don't want to tar -xvf file.tar.xz.


Solution

  • Based on the discussion in the comments, I tried the following which worked for me. It might not be the most optimal solution, the regex might need some improvement, but you'll get the idea.

    I first created a demo archive:

    cd /tmp
    mkdir demo
    for i in {1..100}; do echo $i > "demo/$i.txt"; done
    cd demo && tar cfJ ../demo.tar.xz * && cd ..
    

    demo.tar.xz now contains 100 txt files.

    The following lists the contents of the archive, selects the first file and stores the path within the archive into the variable firstfile:

     firstfile=`tar -tvf demo.tar.xz | grep -Po  -m1  "(?<=:[0-9]{2} ).*$"`
    

    echo $firstfile will output 1.txt.

    You can now extract this single file from the archive:

    tar xf demo.tar.xz $firstfile