Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

So I need to compress a directory with max compression.

How can I do it with xz? I mean I will need tar too because I can't compress a directory with only xz. Is there a oneliner to produce e.g. foo.tar.xz?

share|improve this question

5 Answers 5

up vote 17 down vote accepted

Assuming xz honors the standard set of commandline flags - including compression level flags, you could try:

tar -cf - foo/ | xz -9 -c - > foo.tar.xz 
share|improve this answer
and this uses maximum compression level with XZ? –  LanceBaynes Jan 12 '12 at 21:34
adding -9 to xz will make it max –  bdowning Jan 12 '12 at 21:45
XZ_OPT=-9 tar cJf tarfile.tar.xz directory

tar's lowercase j switch uses bzip, uppercase J switch uses xz.

The XZ_OPT environment variable lets you set xz options that cannot be passed via calling applications such as tar.

This is now maximal

See man xz for other options you can set.

share|improve this answer
and this uses maximum compression level with XZ? –  LanceBaynes Jan 12 '12 at 21:34
It does now, see edited answer and XZ_OPT env var ;) –  bdowning Jan 13 '12 at 0:08
Just a note: you have to export XZ_OPT. –  paraxor Apr 22 '13 at 22:45
No, you don't. That's the whole point. You can set the environment var for just that invocation. You can export it if you want to, but you don't have to. –  bdowning Apr 23 '13 at 9:36
You're assuming bash-like shell for that. –  anddam Apr 29 '13 at 19:56

tar is actually not the best idea for medium-sized files (~10-50 MB): tar preserves creation time, modification time, permission, the directory structure, etc. which takes space.

If this preservation is what you want, then XZ_OPT=-9e tar cJf tarfile.tar.xz directory is precisely what you need.

If you don't care about FS metadata preservation, then give 7zip a try:

7za a -mx=9 directory.7z directory/

(defaults to LZMA2 compression algorithm just like xz... but you could also try PPMd if you have a lot of ASCII/UTF8 text)

Just to give you an idea of the difference:

  • raw data (ASCII text): 18 MB
  • tar: 30 MB (no kidding!)
  • tar.xz: 3.2 MB
  • tar.7z: 3.3 MB
  • 7z: 2.8 MB

I don't know exactly why 7zip can't reach xz level on the tar file, but the difference between tared file and untared file is not that negligible.

share|improve this answer
I guess that 7zip reorder the files so that similar files are grouped together. I.e., text files and image files are compressed using different parameters. A pre-tar-ed archive prevent 7zip from reordering the files. I remember that softwares like WinRAR do it this way, maybe I'm wrong. –  Xiè Jìléi Sep 22 '13 at 6:49

tar command uses J flag for xz files. An example:

tar -cJvf foo.tar.xz foo/

share|improve this answer
The J was already mentioned in bdowning's answer –  Anthon Jan 8 at 22:58

the best is (I checked)

tar cf - wam_GG_${dir}.nc | xz -4e > wam_GG_${dir}.nc.tar.xz 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.