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 18 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 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
XZ_OPT=-9e tar cJf tarfile.tar.xz directory

is even better than

XZ_OPT=-9 tar cJf tarfile.tar.xz directory
share|improve this answer

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.