5

How to convert .tar.gz to .tar.xz?

I have a bunch of .tar.gz files that I would like to convert to .tar.xz for a better compression.

How to do this efficiently? A command like this would be great: How do I convert tar.bz2 to tar.gz?.

Here are the two commands I'm using to create a tar gz and tar xz:

  • TAR GZ tar czf folder.tar.gz folder
  • TAR XZ (LZMA) tar cfJ folder.tar.xz folder

Is there a better way than: tar xvzf folder.tar.gz && tar cfJ folder.tar.xz folder && rm -rf folder?

1

2 Answers 2

3

That command in your nearly duplicate question can be modified to use gunzip as a decompressor and xz to achieve exactly the same result.

gunzip2 -c < file.tar.gz | xz -c -T0 > file.tar.xz

-c in both cases tells the program to output to stdout. In the case of gunzip it receives data from stdin via < file.tar.gz and is then piped (|) to xz. xz then is told the same, with its output piped to your output file. -T0 is there to leverage parallel compression to reduce the operation time significantly.

gunzip should be effectively the same as gzip -d

1
  • what is gunzip2? I can only find gunzip Oct 19, 2022 at 21:17
2

The incantation you seek is:

gzip -cd file.tar.gz | xz > file.tar.xz
rm file.tar.gz

If you have many CPU cores and want maximum possible compression:

gzip -cd file.tar.gz | pxz -9 > file.tar.xz
rm file.tar.gz

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .