12

I want to compress several images into a .xz archive. How do I do that?

0

3 Answers 3

13

Use tar with -J option:

tar -cvJf images.tar.xz /directory/containing/images/*

tar is used to combine multiple files into one (Archive) and then we need to compress the archive using XZ compression algorithm.

From man tar:

-c, --create
       create a new archive
-v, --verbose
       verbosely list files processed
-J, --xz

-f, --file ARCHIVE
       use archive file or device ARCHIVE

Also note that images.tar.xz will be created in the current directory, if you want to save it somewhere else use /full/path/to/images.tar.xz.

12
  • started the process of xzing several huge PSDs and to be honest, the archive is offering some huge compression! 400 MB+ is now 200MB Apr 16, 2015 at 15:28
  • 1
    great..although xz is the slowest among xz, gzip, bzip2 due to its higher compression ratio than the others..the slowness is worth to deal with if you want more compression..
    – heemayl
    Apr 16, 2015 at 15:38
  • My psds in total are, like, 600-900 MB total xD Apr 16, 2015 at 15:39
  • you are good to go....
    – heemayl
    Apr 16, 2015 at 15:40
  • some kinda error ._. Apr 16, 2015 at 15:49
12

Although tar cJf archive files... as detailed by Zacharee1 and by heemayl is usually what you'll want to do, another way is to pipe tarred data to the xz command:

tar c files... | xz > archive.tar.xz

Since Ubuntu's tar supports the J option, this alternate way is specifically useful when you wish to pass options to xz.

In this example, I tar and xzip some TIFF files with a high level of compression (-9 to xz) and verbose output (v to tar, -v to xz):

ek@Io:~/Pictures$ tar vc *.tif{,f} | xz -9v > pics.tar.xz
page001.tif
page002.tif
page003.tif
page004.tif
page9087.tif
page3la.tiff
quux0000.tiff
  100 %       207.3 KiB / 290.0 KiB = 0.715

This could, of course, also be done in two explicitly separate steps:

ek@Io:~/Pictures$ tar vcf pics.tar *.tif{,f}
page001.tif
page002.tif
page003.tif
page004.tif
page9087.tif
page3la.tiff
quux0000.tiff
ek@Io:~/Pictures$ xz -9v pics.tar
pics.tar (1/1)
  100 %       207.3 KiB / 290.0 KiB = 0.715

Those two ways are not actually equivalent in how they operate, though the .tar.xz files they produce in the end should be the same (and were, when I tested it).

  • In the first, the output of tar is piped (|) to the input of xz. xz receives data from tar almost immediately, and no intermediate uncompressed tar file is ever created. This is to say that the first way is essentially equivalent to tar cJf archive files..., except for the additional arguments passed to xz.
  • In the second, an uncompressed tar archive is created by the first command, then compressed by xz in the second command. (xz automatically deletes the original file when it's done, unless invoked with -k/--keep.)

For further reading, see this post by Rafael van Horn and the tar and xz manpages.

3
  • 1
    +1..this is a good one when higher (or varying) compression ratio is desired..
    – heemayl
    Apr 16, 2015 at 18:45
  • 2
    Hello 2015! Another way you can accomplish varying compression levels using tar -J is to specify the environment variable XZ_OPT, making the invocation something like: XZ_OPT="-9v" tar -Jcf out.tar.xz [files]
    – Wug
    Sep 20, 2017 at 21:31
  • 2
    @Wug That's an excellent idea. It seems to me that it warrants its own separate answer. Are you willing to post that? Sep 20, 2017 at 21:37
3

Use this command: tar cJf <archive.tar.xz> <files>. Separate file paths with a space.

14
  • you need this: -cfJ
    – A.B.
    Apr 16, 2015 at 12:56
  • 1
    @A.B.: you don't need - in front..from man tar: A function letter need not be prefixed with ``-'' ..so tar cJf is absolutely OK.
    – heemayl
    Apr 16, 2015 at 13:09
  • @heemayl But it does not seem to be working. I tried it without the '-', but tar is not completed.
    – A.B.
    Apr 16, 2015 at 13:11
  • @heemayl That's what I thought. Apr 16, 2015 at 13:11
  • 1
    Note that you should stick with your first answer tar cJf, that would give OP another way of doing this as my answer already covered tar --cvJf at first..
    – heemayl
    Apr 16, 2015 at 13:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .