Create a tar.xz in one command

183,555

Solution 1

Use the -J compression option for xz. And remember to man tar :)

tar cfJ <archive.tar.xz> <files>

Edit 2015-08-10:

If you're passing the arguments to tar with dashes (ex: tar -cf as opposed to tar 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:

tar -cJf <archive.tar.xz> <files>

Solution 2

Switch -J only works on newer systems. The universal command is:

To make .tar.xz archive

tar cf - directory/ | xz -z - > directory.tar.xz

Explanation

  1. tar cf - directory reads directory/ and starts putting it to TAR format. The output of this operation is generated on the standard output.

  2. | pipes standard output to the input of another program...

  3. ... which happens to be xz -z -. XZ is configured to compress (-z) the archive from standard input (-).

  4. You redirect the output from xz to the tar.xz file.

Solution 3

If you like the pipe mode, this is the most clean solution:

tar c some-dir | xz > some-dir.tar.xz

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 for xz, because it's default.

It works with gzip and bzip2 too:

tar c some-dir | gzip > some-dir.tar.gz

or

tar c some-dir | bzip2 > some-dir.tar.bz2

Decompressing is also quite straightforward:

xzcat tarball.tar.xz | tar x
bzcat tarball.tar.bz2 | tar x
zcat tarball.tar.gz | tar x

If you have only tar archive, you can use cat:

cat archive.tar | tar x

If you need to list the files only, use tar t.

Solution 4

Quick Solution

tarxz() { tar cf - "$1" | xz -4e > "$1".tar.xz ; }
tarxz name_of_directory

(Notice, not name_of_directory/)


Using xz compression options

If you want to use compression options for xz, or if you are using tar on MacOS, you probably want to avoid the tar -cJf syntax.

According to man xz, the way to do this is:

tar cf - filename | xz -4e > filename.tar.xz

Because I liked Wojciech Adam Koszek's format, but not information:

  1. c creates a new archive for the specified files.
  2. f reads from a directory (best to put this second because -cf != -fc)
  3. - outputs to Standard Output
  4. | pipes output to the next command
  5. xz -4e calls xz with the -4e compression option. (equal to -4 --extreme)
  6. > filename.tar.xz directs the tarred and compressed file to filename.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 set xz 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):

xz -dc filename.tar.xz | tar x

Note: unlike Rafael's answer, use xz -dc instead of catxz. The docs recommend this in case you are using this for scripting. Best to have a habit of using -d or --decompress instead of unxz as well. However, if you must, using those commands from the command line is fine.

Solution 5

I can never remember which archive switch does what, so these days, I prefer the "auto-compress" feature in newer tar versions (-a or --auto-compress). The command then simply looks like this:

tar caf file.tar.xz file

With that -a option, tar deduces the compression to use automatically from the file ending used for the archive!

Share:
183,555

Related videos on Youtube

George K.
Author by

George K.

Updated on December 03, 2021

Comments

  • George K.
    George K. over 2 years

    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.

  • Engineer2021
    Engineer2021 about 10 years
    +1 On RHEL 5.10, I don't see a J option, but this works. I think you need RHEL6.
  • Eliah Kagan
    Eliah Kagan about 9 years
    -f does not mean "from file" when passed to xz. Instead it is short for --force (see xz(1) for details), and is best not used unless needed.
  • Stuart Cardall
    Stuart Cardall over 8 years
    adding v to the command switches (tar -cJvf) to be verbose shows the files being added to the archive.
  • mwfearnley
    mwfearnley over 7 years
    Why not adjust the commands to pipe the output from tar to xz?
  • mwfearnley
    mwfearnley over 7 years
    Given the ordering is important with the dash, it's probably best to assume the ordering is always important, and put the f last, even without the dash.
  • chmike
    chmike about 7 years
    Is it possible to pass xz parameters like -e (--extended) ? Apparently it wont use extended by default.
  • Jack Q
    Jack Q over 6 years
    @mwfearnley I think the note is merely to emphasize that the commands required to be executed sequentially. Since the file name is used in both command, they won't write or read data from standard output or input. If using the command without specifying the filename, they can be chained up, like tar -c file-to-compress | xz -z > file.tar.xz.
  • Wojciech Adam Koszek
    Wojciech Adam Koszek over 6 years
    @EliahKagan Fixed. Thanks.
  • cbarrick
    cbarrick about 6 years
    tar on macOS seems to support -J but the feature is not given in the man page. Had to look it up here. Though they do mention XZ in the section on --options...
  • Lo-Tan
    Lo-Tan over 5 years
    Any particular reason for not using the tar -cJf syntax on MacOS? Just curious, because I was doing that just now and it seemed to be working.
  • Connor
    Connor over 5 years
    @Lo-Tan It works if you have tar. When I was using it I didn't have the right version of tar so it didn't work for me. I also wanted to use the compression options for xz, which you can't do if you use tar. Also, the piping syntax is easier for me to remember, so I tend to use that. But to each their own
  • oidualc
    oidualc over 5 years
    To use multithreaded compression the option -T0 can be used: tar cf - directory/ | xz -z -T0 - > directory.tar.xz
  • fgwaller
    fgwaller over 5 years
    Another advantage is that you can easily specify the compression level or any other option to xz on the command line. For example if you want to speed up the compression and do not care that much about the size you can use -1 -T0 or -0 -T0 as options, which will usually still give you a smaller file than gzip in a comparable or faster time, while the default -6 is considerably slower than gzip.
  • Luis Lavaire.
    Luis Lavaire. almost 4 years
    Using & will not launch a command one after another. Instead, it will launch the first one as a background process.
  • Dzenly
    Dzenly almost 4 years
    @chmike Use XZ_OPT environment variable. E.g. XZ_OPT="-9e -T0".
  • Phillip Schmidt
    Phillip Schmidt over 3 years
    You probably mean &&, which runs commands sequentially. It also stops executing if any of the individual commands returns an error code