FactorPad
Faster Learning Tutorials

Linux xz command summary with examples

xz allows you to compress and decompress files.
  1. Purpose - Learn what xz is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with xz.
  4. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, November 6, 2016
Updated: July 23, 2018
In this tutorial, 71 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Next

~/ home  / tech  / linux essentials  / xz command


The Linux xz command

Beginner

Learn to compress and decompress files at the Linux command line.

Video Tutorial

Linux xz command summary with examples (3:56)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our seventy-first word, or command to memorize is xz from our category Files.

xz allows you to compress and decompress files.

Recall from the three videos on gzip, gunzip, and bzip2, we've been evaluating compression algorithms and noting some consistencies between their usage. Now without going too deep, I'll introduce another.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The xz command has over 30 options and the argument is the file or list to process, for files with extensions .xz or .lzma.

Like most commands, help is available with double-dash --help, -d decompresses and -v shows file compression details.

So why is xz an important command? Well, if your organization compresses files with .xz or .lzma extensions then you need to be able to handle them. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. First I'll take you to the man page for xz for a quick point.

$ man xz
XZ(1) XZ Utils XZ(1) NAME xz, unxz, xzcat, lzma, unlzma, lzcat - Compress or decompress .xz and .lzma files SYNOPSIS xz [option]... [file]... unxz is equivalent to xz --decompress. xzcat is equivalent to xz --decompress --stdout. lzma is equivalent to xz --format=lzma. unlzma is equivalent to xz --format=lzma --decompress. lzcat is equivalent to xz --format=lzma --decompress --stdout. When writing scripts that need to decompress files, it is recommended to always use the name xz with appropriate arguments (xz -d or xz -dc) instead of the names unxz and xzcat. DESCRIPTION xz is a general-purpose data compression tool with command line syntax similar to gzip(1) and bzip2(1). The native file format is the .xz format, but the legacy .lzma format used by LZMA Utils and raw compressed streams with no container format headers are also sup- ported. xz compresses or decompresses each file according to the selected operation mode. If no files are given or file is -, xz reads from standard input and writes the processed data to standard output. xz will refuse (display an error and skip the file) to write com- pressed data to standard output if it is a terminal. Similarly, xz will refuse to read

Notice in the synopsis here, all 5 of these commands are covered in this one man page and all have these xz equivalents, which can be confusing. (Hit q to quit)

Next, for consistency across videos, let's copy that same file video69b.txt and call it video71.txt, and compress it with xz -v

$ cp video69b.txt video71.txt $ xz -v video71.txt video71.txt (1/1) 100 % 6,112 B / 17.3 KiB = 0.344

And compression says 0.344, which is equivalent to 65.6%, compared to, what was it? 64% for gzip and 67% for bzip2. For some this is a meaningful difference but for most it isn't.

And last, let's decompress using xz -dk on video71.txt.xz, keeping the old file, and an ls -og video71* and like before we should see two files.

$ xz -dk video71.txt.xz $ ls -og video71* -rw-r--r-- 1 17752 Nov 6 16:07 video71.txt -rw-r--r-- 1 6112 Nov 6 16:07 video71.txt.xz

A Final Tip

Okay now you know how to use xz. And you know the syntax for commands, options and arguments.

One last tip about the xz command. So it's good that there are similarities across compression choices, however, many related commands do the same thing as we saw in the man command. So I advise, if possible select one and stick with it.

Okay, thanks for visiting today. I hope this was a helpful introduction to the xz command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

As always, reach us on social media, including our YouTube Channel. Here's the easiest way to subscribe.

  • To see the Outline of all 100 tutorials, click Outline.
  • To go back to the bzip2 command, hit Back.
  • To review the most common file compression mechanism for many Windows and Mac users with the zip command, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / xz command



 
 
Keywords:
Linux xz command
xz Linux
unzip xz file
compress files
compress directories
decompress files
decompress directories
open xz
open lzma
extract zz
extract lzma
open xz
open lzma
extract lzma
xz example
file compression