I am trying to create a .tar.xz compressed archive in one command. What is the specific syntax for that?
I have tried tar cf - file | xz file.tar.xz>
tarxz() { tar cf - "$1" | xz -4e > "$1".tar.xz ; }
tarxz name_of_directory
(Notice, not name_of_directory/)
If you want to use compression options for xz, or if you are using tar on MacOS, you probably want to avoid the tar -cJf syntax.
According to man xz, the way to do this is:
tar cf - filename | xz -4e > filename.tar.xz
Because I liked Wojciech Adam Koszek's format, but not information:
c creates a new archive for the specified files.f reads from a directory (best to put this second because -cf != -fc)- outputs to Standard Output| pipes output to the next commandxz -4e calls xz with the -4e compression option. (equal to -4 --extreme)> filename.tar.xz directs the tarred and compressed file to filename.tar.xzwhere -4e is, use your own compression options.
I often use -k to --keep the original file and -9 for really heavy compression. -z to manually set xz to zip, though it defaults to zipping if not otherwise directed.
To echo Rafael van Horn, to uncompress & untar (see note below):
xz -dc filename.tar.xz | tar x
Note: unlike Rafael's answer, use xz -dc instead of catxz. The docs recommend this in case you are using this for scripting. Best to have a habit of using -d or --decompress instead of unxz as well. However, if you must, using those commands from the command line is fine.