964

I'm used to extracting tarballs with a -xfz flag, which handles gzip and bzip2 archives.

Recently I've run into a .tar.xz file and I would like to uncompress it in one step using tar, how can I do that?

BuZZ-dEE
  • 13,993
  • 18
  • 63
  • 80
Jorge Castro
  • 70,934
  • 124
  • 466
  • 653
  • 14
    note you may have to install `xz-utils` if not already present – Tobias Kienzler May 10 '13 at 07:04
  • here's my little script that guesses tar flags for you: https://gist.github.com/shime/5908634 – shime Jul 02 '13 at 12:06
  • 6
    `tar --help` lists `tar` flags. `-xzf` applies to `gzip`. `-xjf` to `bz2`. `-xJf` to `xz`. – noobninja Nov 11 '16 at 19:27
  • Better Question to ask than how to do this with tar: Use `unar` or `7z` and never worry about choosing the right program for your type of archive again. This is the only feasible solution looking forward with more and more archive types coming. Unless you care about the technical details... – masterxilo Mar 21 '20 at 18:05
  • https://pypi.org/project/unp/ – Andrew Jan 17 '22 at 19:35

12 Answers12

1225

Ubuntu includes GNU tar, which recognizes the format by itself! One command works with any supported compression method, per the manual.

# The same command handles any compression format! Ex:

tar xf archive.tar.xz  # for .tar.xz files
tar xf archive.tar.gz  # for .tar.gz files
tar xf archive.tar     # for .tar files

etc. If tar gives a Cannot exec error, you may need to sudo apt install xz-utils first.

Gabriel Staples
  • 8,025
  • 7
  • 66
  • 105
ramslök
  • 12,470
  • 1
  • 13
  • 3
  • I think that's with bsdtar :) – Savvas Radevic Feb 26 '12 at 17:46
  • 45
    It's a feature of GNU tar. I don't know about competing implementations, but GNU tar should be the most relevant to ubuntu. http://www.gnu.org/software/tar/manual/tar.html#SEC131 – ramslök Feb 29 '12 at 02:20
  • 50
    if you run into `tar: xz: Cannot exec: No such file or directory`, install `xz-utils`: `sudo apt-get install xz-utils` – Collin Anderson Feb 11 '14 at 16:33
  • 3
    If you cant remember that, it stands for, and is the same as: `tar --extract --file archive.tar.xx` – felbus Jan 15 '15 at 16:58
  • 12
    This is not an answer, it is a 'you don't care about the answer, even though you asked' response. Spare a thought for people who are not on 'latest' – Sean Houlihane Jan 20 '15 at 09:41
  • 6
    What version of `tar` no longer requires the specific flag? – Mark Tomlin May 13 '15 at 19:19
  • 10
    @SeanHoulihane, it is not an answer because the OP asked a minor [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). ramslök gave the OP better than was asked for, and that included an effective "You don't care about the answer, even though you asked", which was appropriate because it was true. – Christos Hayward Sep 05 '15 at 22:49
  • I had to use "-" in front of the "xf" – DrCord Mar 25 '16 at 19:36
  • 1
    o tar how i love thee - tape archive - it literally means tape archive and we are STILL using it, fantastic piece of code – tofutim Mar 17 '18 at 23:45
  • 2
    @MarkTomlin and others who ask, compressed archive autodetection was added in GNU tar 1.15, released December 2004. – fkraiem Oct 15 '18 at 03:02
  • @fkraiem You're a necro-answering super star. – Mark Tomlin Oct 15 '18 at 03:36
  • @MarkTomlin I just saw this because it was bumped due to someone (not me) answering. ;) – fkraiem Oct 15 '18 at 03:52
  • 3
    This answer didn't work for me, got a `tar: This does not look like a tar archive` error. Mathias Bynens answer, however, did work. – virtualxtc Apr 11 '19 at 06:44
  • Any way to show a progress bar? – Gabriel Staples Mar 28 '22 at 07:18
393

Try

tar -xJf file.pkg.tar.xz

The -J is the flag that specifically deals with .xz files.

Jorge Castro
  • 70,934
  • 124
  • 466
  • 653
jrg
  • 60,101
  • 54
  • 172
  • 246
94

If for some reason the tar solutions don’t work (perhaps because you’re using the OS X built-ins), try this:

unxz < file.tar.xz > file.tar

…which is equivalent to:

xz -dc < file.tar.xz > file.tar

Then use tar to untar the file.

Mathias Bynens
  • 1,049
  • 9
  • 9
  • 3
    That should be constructable with a pipe. – gerrit Mar 20 '14 at 18:43
  • 7
    I feel like you could just do `unxz < file.tar.xz | tar x` or similar. – thirtythreeforty May 16 '14 at 14:39
  • 7
    this worked for me, where `tar xf` did not. ubuntu 12.04 – philshem Aug 14 '14 at 20:57
  • At least on my Ubuntu machine, `unxz` is not equivalent to `xz -dc`, but to `xz -d`. So to extract `file.tar.xz` to `file.tar`, you'd simply write `unxz file.tar.xz`. If you want an equivalent to `xz -dc`, decompressing to stdout, use `xzcat`. For example `xzcat file.tar.xz | tar x`. – nwellnhof May 25 '17 at 16:50
42

xz is a lossless data compressor. You will have to extract the tar ball from xz and then extract the tar:

unxz my_archive.tar.xz      # results in my_archive.tar

Then you know to extract a tar

tar -xf my_archive.tar

Source: XZ Utils - Wikipedia.

Seth
  • 57,282
  • 43
  • 144
  • 200
Foxx
  • 421
  • 4
  • 2
  • 2
    Best answer. `xz` doesn't need stdout redirection. Even better: `unxz -k` – Mark Jeronimus Feb 27 '17 at 09:29
  • @MarkJeronimus I don’t find this so good since it creates an intermediate file. Using [GNU tar](https://askubuntu.com/a/107976/250300) or a [pipe](https://askubuntu.com/questions/92328/how-do-i-uncompress-a-tarball-that-uses-xz#comment618114_407911) should demand less resources. – Melebius Jul 02 '19 at 07:44
  • 2
    This worked for me, but it should be noted that it will delete the original tar.xz file – Kelly Bang Feb 29 '20 at 03:05
  • Yes, this is a straight way to do. 1000 up votes. – le hien Jun 25 '21 at 06:07
21

I had the same problem, the tar xf command was not able to extract it. To fix this, you need to install the xz-utils package. The solution was:

sudo apt-get install xz-utils

then:

tar xf myfile.tar.xz
Zsolt Sky
  • 311
  • 2
  • 3
20
tar -xvf package.tar.xz

-x - extract files

-v - verbosely list files processed

-f - use specified archive file

nsane
  • 476
  • 1
  • 4
  • 21
6

Just want to add that if you have an old version of GNU tar prior to version 1.22 when the --xz and -J options became available, you could compress or decompress tar.xz files by using --use-compress-program xz. For example,

tar --use-compress-program xz -cf example.tar.xz file1 file2 file3

or

tar --use-compress-program xz -xf example.tar.xz
Derek Veit
  • 103
  • 3
ScottH
  • 71
  • 1
  • 2
5

I like dtrx

sudo apt install dtrx
dtrx your-file.tar.xz
Boris Verkhovskiy
  • 3,134
  • 3
  • 30
  • 39
5

If tar recognizes the compression format, you don't need a flag:

tar xvf *.tar.xz

If you need to decompress the input manually, for example because your tar is too old to recognize xz, or you need a special path:

xz -cd *.tar.xz | tar xvf -

Pipes are faster than making an uncompressed intermediate file, and use less disk space too!

Orion Lawlor
  • 495
  • 5
  • 5
4

Ubuntu comes with Python (Python 2.7 and Python 3), which contains the necessary modules for extracting archives. So if for whatever reason tar command is missing (say your sysadmin has removed it and you don't have sudo privillege to install it), one can use:

python3 -c 'import tarfile,sys; b = tarfile.open(sys.argv[1]);print(b.extractall())' ./archive.xz 

As a short script,that's more readable as:

#!/usr/bin/env python3
import tarfile,sys

with tarfile.open( sys.argv[1] ) as fd:
    fd.extractall()

Suppose I created an .xz file with tar cJf thing.xz /etc/passwd. The archive will contain etc directory with passwd file inside. Using the above script will result in etc directory created in your current working directory, and within it will be passwd file. Of course, this can always be extended by specifying path where you want to extract inside the extractall() function.

Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
  • The [`tarfile` documentation](https://docs.python.org/3/library/tarfile.html#tarfile.TarFile.extractall) warns that filenames with an absolute path will extract to the specified directory, not relative to your current working directory as claimed in your answer. – Mark Ransom Jan 05 '23 at 17:04
4

Wow, that's a really good one. Was it done with 7zip on a Mac? Try this:

7z x -so file.tar.xz | tar xf -
  • Requires 7z, which isn't what he wants - he wants to do it entirely in tar. – jrg Jan 03 '12 at 00:08
  • Yes, thanks - the "xz" got me! Well, it's one step anyway :) And `tar J` = `tar xz`, so we might even write `tar xzf file.tar.xz` like "normal" `tar xvfz file.tar.gz`. So basically no difference. No dash needed before using the switch. –  Jan 03 '12 at 00:19
  • It's almost like the answer was given in the question. :) –  Jan 03 '12 at 00:26
  • I was thrown off too because tar zxf errored out on the .xz file, I suppose just using J all the time would be the way to go. – Jorge Castro Jan 03 '12 at 00:28
  • 2
    Yup! The manual page is not in sync with the source: buffer.c uses -J for lzma. –  Jan 03 '12 at 00:45
1

unar is quite a nice simple program and easy to type, to unarchive almost any format including 7z and RAR

it seems to be written in (GNU) Objective-C and so requires installation of some gnustep (GNU's Objective-C implementation) libraries (gnustep-base-runtime and libgnustep-base1.25)

if you use --install-suggests or have configured apt to install suggested packages, unar will suggest and install many GUI GNUStep programs which are not what you want

sudo apt install --no-install-suggests unar 
unar linux-source.tar.xz

it will create the output directory linux-source automatically.

cat
  • 1,632
  • 1
  • 24
  • 47
  • 1
    Best answer (though the question specifically mentions using tar for the job...). Since installing this I never have to look up how to unpack archive X again. All that knowledge is encoded in that program. This is exactly how everything should *just work together*... – masterxilo Mar 21 '20 at 18:07