Shell – Unpack file compressed in .txz and .tar with one command

compressionshelltar

I have a file compressed in *.txz. After unpacking it I received a *.tar file. Is there any way to unpack it twice with one command? I mean unpack file (*.tar).txz with one command?

For know I'm do it like this:

xz -d file.txz
tar xvf file.tar

But I wonder if there is nicer way.

Best Answer

xz -d < file.tar.xz | tar xvf -

That's the same as with any compressed archive. You should never have to create an uncompressed copy of the original file.

Some tar implementations like recent versions of GNU tar have builtin options to call xz by themselves.

With GNU tar or bsdtar:

tar Jxvf file.tar.xz

Though, if you've got a version that has -J, chances are it will detect xz files automatically, so:

tar xvf file.tar.xz

will suffice.

If your GNU or BSD tar is too old to support xz specifically, you may be able to use the --use-compress-program option:

tar --use-compress-program=xz -xvf file.tar.gz

One of the advantages of having tar invoke the compressor utility is that it is able to report the failure of it in its exit status.

Note: if the tar.xz archive has been created with pixz, pixz may have added a tar index to it, which allows extracting files individually without having to uncompress the whole archive:

pixz -x path/to/file/in/archive < file.tar.xz | tar xvf -
Related Question