Create a tar.xz in one command

前端 未结 5 2024
逝去的感伤
逝去的感伤 2021-01-29 18:32

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

5条回答
  •  遇见更好的自我
    2021-01-29 19:01

    Quick Solution

    tarxz() { tar cf - "$1" | xz -4e > "$1".tar.xz ; }
    tarxz name_of_directory
    

    (Notice, not name_of_directory/)


    Using xz compression options

    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:

    1. c creates a new archive for the specified files.
    2. f reads from a directory (best to put this second because -cf != -fc)
    3. - outputs to Standard Output
    4. | pipes output to the next command
    5. xz -4e calls xz with the -4e compression option. (equal to -4 --extreme)
    6. > filename.tar.xz directs the tarred and compressed file to filename.tar.xz

    where -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 uncompress and untar

    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.

提交回复
热议问题