How to Read Compressed xz Files Using the xzless Command in Linux

When you compress a massive log file using the modern xz utility in Linux, the resulting file is encoded in a highly compressed binary format. If you try to open it with standard text viewing tools like cat or less, your terminal screen will instantly fill with unreadable, corrupted characters. While you could decompress the entire file back into a massive text document just to read it, doing so wastes time and hard drive space. Instead, you can use the xzless command to read the compressed data seamlessly.

How the xzless Command Works

The xzless utility acts as a specialized bridge between the xz decompression algorithm and the standard less terminal pager. When you run it against a compressed file, it decompresses the data directly into your computer’s RAM, rather than writing it to the hard drive, and immediately pipes that readable text to your screen.

To read a compressed log file named apache_error.log.xz, simply run:

xzless apache_error.log.xz

Navigating the Pager Interface

Because the output is routed through the standard less pager, you get access to all of its powerful navigation tools. This is the primary advantage of xzless over its older sister command, xzmore.

Once the file opens in your terminal, you can navigate the text using these keyboard shortcuts:

  • Arrow Keys: Use the Up and Down arrow keys to scroll through the text one line at a time. This is a crucial feature that xzmore lacks.
  • Spacebar: Press the Spacebar to jump forward exactly one full page (or screen) of text.
  • b: Press the ‘b’ key to jump backward exactly one full page.
  • g and G: Press the lowercase ‘g’ key to jump instantly to the very top (beginning) of the document. Press the uppercase ‘G’ (Shift + g) to jump to the very bottom (end) of the document.

Searching for Text Inside Compressed Files

Perhaps the most powerful feature of the xzless interface is its ability to perform deep keyword searches on the data while it is still technically compressed on your hard drive.

  1. While viewing the file in xzless, type a forward slash (/). This will open a small search prompt at the bottom left corner of your terminal.
  2. Type the word you are looking for (e.g., /error 404) and press Enter.
  3. The pager will instantly jump to the first occurrence of that phrase and highlight it.
  4. To jump to the next occurrence of the phrase, simply press the lowercase n key. To jump to the previous occurrence, press the uppercase N key.

Once you are finished reading and searching, press the q key to quit the pager and return to your standard bash prompt. The original .xz file remains perfectly compressed and untouched on your drive.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.