I am trying to create a .tar.xz
compressed archive in one command. What is the specific syntax for that?
I have tried tar cf - file | xz file.tar.xz
, but that does not work.
I am trying to create a .tar.xz
compressed archive in one command. What is the specific syntax for that?
I have tried tar cf - file | xz file.tar.xz
, but that does not work.
Switch
-J
only works on newer systems. Universal command is:To make .tar.xz archive
Explanation
tar cf - directory
reads directory/ and starts putting it to TAR format. Output of this operation is generated on the standard output.|
pipes standard output to input of another program...... which happens to be
xz -zf -
. XZ is configured to create (-z
) the archive from file (-f
) which happens to be standard input (-
).You redirect the output from
xz
to thetar.xz
file.Use the
-J
compression option forxz
. And remember toman tar
:)Edit 2015-08-10:
If you're passing the arguments to
tar
with dashes (ex:tar -cf
as opposed totar cf
), then the-f
option must come last, since it specifies the filename (thanks to @A-B-B for pointing that out!). In that case, the command looks like:If you like the pipe mode, this is the most clean solution:
It's not necessary to put the
f
option in order to deal with files and then to use-
to specify that the file is the standard input. It's also not necessary to specify the-z
option forxz
, because it's default.It works with
gzip
andbzip2
too:or
Decompressing is also quite straightforward:
If you have only
tar
archive, you can usecat
:If you need to list the files only, use
tar t
.Using xz compression options
If you want to use compression options for
xz
, or if you are usingtar
on MacOS, you probably want to avoid thetar -cJf
syntax.According to
man xz
, the way to do this is:tar cf - baz | xz -4e > baz.tar.xz
Because I liked Wojciech Adam Koszek's format, but not information:
c
creates a new archive for the specified files.f
reads from a directory (best to put this second because-cf
!=-fc
)-
outputs to Standard Output|
pipes output to the next commandxz -4e
callsxz
with the-4e
compression option. (equal to-4
--extreme
)> baz.tar.xz
directs the tarred and compressed file tobaz.tar.xz
where
-4e
is, use your own compression options. I often use-k
to--keep
the original file and-9
for really heavy compression.-z
to manually setxz
to zip, though it defaults to zipping if not otherwise directed.To uncompress and untar
To echo Rafael van Horn, to uncompress & untar (see note below):
Note: unlike Rafael's answer, use
xz -dc
instead ofcatxz
. The docs recommend this in case you are using this for scripting. Best to have a habit of using-d
or--decompress
instead ofunxz
as well. However, if you must, using those commands from the command line is fine.Try this:
tar -cf file.tar file-to-compress ; xz -z file.tar
Note:
|
because this runs commands simultaneously. Using;
or&
executes commands one after another.