Create tar.xz in one command

I am trying to create a compressed .tar.xz archive in one command. What is the specific syntax for this?

I tried tar cf - file | xz file.tar.xz tar cf - file | xz file.tar.xz , but this does not work.

+76
compression tar archive xz
Sep 17 '13 at 17:08
source share
4 answers

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

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

Edit 2015-08-10:

If you pass tar arguments with a dash (for example: tar -cf as opposed to tar cf ), then the -f option should be the last as it specifies the file name (thanks to @ABB for pointing this out!). In this case, the command looks like this:

 tar -cJf <archive.tar.xz> <files> 
+125
Sep 17 '13 at 17:12
source share

Switch -J only works on newer systems. Universal team:

Make a .tar.xz archive

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

Explanation

  • tar cf - directory reads the / directory and starts putting it in the TAR format. The output of this operation is generated at the standard output.

  • | outputs standard output to the input of another program ...

  • ... which turns out to be xz -zf - . XZ is configured to create ( -z ) an archive from a file ( -f ), which is standard input ( - ).

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

+25
Jul 10 '14 at 0:31
source share

If you like the pipe mode, this is the cleanest solution:

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

No need to set the f parameter to process files, and then use - to indicate that the file is standard input. There is also no need to specify the -z option for xz , because it is the default.

It also works with gzip and bzip2 :

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

or

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

Decompression is also quite simple:

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

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

 cat archive.tar | tar x 

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

+21
Jan 05 '15 at 18:11
source share

Try the following: tar -cf file.tar file-to-compress ; xz -z file.tar tar -cf file.tar file-to-compress ; xz -z file.tar

Note:

  • tar.gz and tar.xz do not match; xz provides better compression.
  • Do not use pipe | because it runs commands at the same time. Use ; or & executes commands one by one.
+5
Mar 10 '14 at 13:03
source share



All Articles