Ubuntu – How to compress multiple files into a .xz archive
compression
I want to compress several images into a .xz archive. How do I do that?
Best Answer
Although tar cJf archivefiles... as detailed by Zacharee1 and by heemayl is usually what you'll want to do, another way is to pipe tarred data to the xz command:
tar c files... | xz > archive.tar.xz
Since Ubuntu's tar supports the J option, this alternate way is specifically useful when you wish to pass options to xz.
In this example, I tar and xzip some TIFF files with a high level of compression (-9 to xz) and verbose output (v to tar, -v to xz):
Those two ways are not actually equivalent in how they operate, though the .tar.xz files they produce in the end should be the same (and were, when I tested it).
In the first, the output of tar is piped (|) to the input of xz. xz receives data from tar almost immediately, and no intermediate uncompressed tar file is ever created. This is to say that the first way is essentially equivalent to tar cJf archivefiles..., except for the additional arguments passed to xz.
In the second, an uncompressed tar archive is created by the first command, then compressed by xz in the second command. (xz automatically deletes the original file when it's done, unless invoked with -k/--keep.)
tar lives up to its name "Tape ARchiver". It takes a directory tree, and writes it out to a single file (which could be a tape drive), preserving the directory structure and metadata.
So, yes, very similar to zip, although without built-in compression, and with a full understanding of unix metadata.
The unix philosophy is that each tool should do one thing, and do it well. Tar can archive a directory, and the you can compress the archive with a compression tool like gzip, bzip2, or xz.
Best Answer
Although
tar cJf archive files...
as detailed by Zacharee1 and by heemayl is usually what you'll want to do, another way is to pipe tarred data to thexz
command:Since Ubuntu's
tar
supports theJ
option, this alternate way is specifically useful when you wish to pass options toxz
.In this example, I tar and xzip some TIFF files with a high level of compression (
-9
toxz
) and verbose output (v
totar
,-v
toxz
):This could, of course, also be done in two explicitly separate steps:
Those two ways are not actually equivalent in how they operate, though the
.tar.xz
files they produce in the end should be the same (and were, when I tested it).tar
is piped (|
) to the input ofxz
.xz
receives data fromtar
almost immediately, and no intermediate uncompressed tar file is ever created. This is to say that the first way is essentially equivalent totar cJf archive files...
, except for the additional arguments passed toxz
.xz
in the second command. (xz
automatically deletes the original file when it's done, unless invoked with-k
/--keep
.)For further reading, see this post by Rafael van Horn and the
tar
andxz
manpages.