Try DigitalOcean free for up to two months!

Applications 32 – XZ Compression – Basic Options

Discussion in 'Installation' started by Jarret W. Buse, Aug 30, 2015.

  1. Jarret W. Buse

    Jarret W. Buse Well-Known Member Staff Writer

    Jul 3, 2013
    Likes Received:
    Applications 32 – XZ Compression – Basic Options

    Many compression utilities exist for Linux. The “xz” compression utility is a lossless data compression program which incorporates the LZMA/LZMA2 compression algorithms. The “xz” utility may be a subject to know for the Linux+ exam.

    The lossless data compression is the method of compression which does not lose any content and can be reconstructed to its original form. Similar to “bzip2”, the “xz” application only compresses single files into an archive. If multiple files are specified, each file will be compressed and the filename will have an added extension of “.xz”. To compress numerous files, you should first archive the files with a program such as “tar” and then compress the tar. The “xz” program compresses a single file and keeps the filename, but adds the “.xz” file extension. To compress numerous files together, use another program such as “tar” to join files together and then compress them using “xz”.

    The syntax for “xz” usage:

    xz [parameters] [files]

    The various parameters follow with examples.

    • -z (--compress) – cause files to be compressed (default)

    If no parameters are given, the program will compress the files listed. For example, to compress a file called “database.db”, the command would be “xz -z database.db”.

    NOTE: Be aware that without the parameter “-k”, the original file will be deleted.

    • -d (--decompress) - force the file from the existing “xz” file to be extracted from the archive

    The “-d” option extracts the file from within the “xz” compressed file. For example, to extract the “database.db” file from the “database.db.xz”, the command would be “xz -d database.db.xz”.

    • -t (--test) – test the integrity of the compressed file

    To verify the “xz” file was created correctly, the “-t” option is used and the compressed (“xz”) file is specified. For example, to test the “datbase.db.xz”, file use the command “xz -t database.db.xz”. If the file's integrity checks fine, then no message is shown. Messages will be displayed if an error occurs.

    • -l (--list) - show information about the specified .xz file

    To list information about the compressed “xz” file, the parameter “-l” is used. The information returned is:

    1. Number of streams
    2. Number of blocks
    3. Compressed size
    4. Uncompressed size
    5. Ratio
    6. Check
    7. Check information

    For example, to view more information about the “database.db.xz” file, the command to use would be “xz -l database.db.xz”.

    • -k (--keep) – do not delete the file to be compressed when done

    When not setting this option, the file being compressed will be deleted when the “xz” file is made. Deleting the file is the default when making a compressed file. For example, if we need to compress the “database.db” file, yet keep for access by users, the “-k” option is needed. So, to backup the database and keep it in place for use by the users, the command is “xz -k database.db”.

    • -f (--force) – forces the overwriting of the output file

    If an extracted file from an archive is to replace an existing file, the option “-f” needs to be used. Without the “-f” option, no existing file will be overwritten. If the file does exist and the “-f” option is not used, an error stating the file already exists will be shown. For example, to overwrite the “database.db” file from the “database.db.xz” archive and overwrite the file, the command is “xz -df database.db.xz”.

    • -c (--stdout) – writes the contents of the file in the archive to standard output and doesn't delete the archive

    If you need to see the contents of the compressed file, in an uncompressed state, then use the “-c” option. The “-c” option must be used with the option “-d”. The archive is not truly uncompressed to disk, only to stdout. To view the contents of the “database.db” in the archive “database.db.xz”, the command is “xz -dc database.db.xz”.

    • -0 ... -9 – set compression level, default is 6. 0 is storage and 9 is the highest compression level. Be careful using compression levels 7 to 9 because of memory usage

    By default, no compression level needs to be set since the default is 6. The smaller the number, the less resources are needed to compress the file. The higher the number the more resources are needed to create the archive, but the file may be smaller. If a higher level is used, the more resources may be needed when uncompressing the file. For example, to compress the “database.db” file to its smallest size with xz, the command would be “xz -z9 datbase.db”.

    • -e (--extreme) - try to improve compression ratio by using more CPU time

    The option “-e” uses more CPU when compressing the file possibly helping the compression ratio. When decompressing the file, more CPU is not needed to process the archive. To compress a file, “database.db”, with extreme compression, use the command “xz -ze database.db”.

    • -q (--quiet) - suppress warnings with one “-q” and to also suppress errors use “-qq”

    When using “xz” and you do not want any errors or warnings to appear, such as when using a script, the option can be useful. To uncompress the file “database.db.xz” and show no errors or warnings, the command would be “xz -dqq database.db.xz”.

    • -v (--verbose) – show more details by using “-v” and more details with the option “-vv”

    Sometimes more details are needed since some options provide no output at all, such as creating an archive. When creating an archive, no details are shown unless they are warnings or errors. To provide details for these options, use the “-v” or “-vv” options. For example, to show details when creating the archive of the “database.db” file, the command would be “xz -zvv database.db”.

    • -V (--version) – shows the version number of the “xz” application and exits

    To see the current version of the “xz” program use the “-V” option as follows “xz -V”.

    • -h (--help) - displays the help information for the basic options and exits

    To see a list of the basic options, those covered in this article, use the command “xz -h”.

    • -H (--long-help) - displays the help information for the longer options and exits

    To see a list of the longer options, those covered in the next article, use the command “xz -H”.
    Be sure to understand the parameters covered in this article and how they work.

    Attached Files:

    Marc Arribas and DevynCJohnson like this.

Share This Page