Read Compressed Text Safely with xzless
You will finish with a quick way to inspect an .xz or .lzma text file without unpacking a second copy. xzless decompresses the input for less, so you can scroll, search and quit using familiar pager controls. The examples match the installed XZ Utils command here: /usr/bin/xzless reports version 5.4.5, supplied by the distro's xz-utils package version 5.6.1+really5.4.5-1ubuntu0.3.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a shell, xz-utils, less, and a compressed text file. This guide only reads data. It does not overwrite the archive or create an extracted file.
1. Check which xzless will run
Start by checking the command selected by your PATH:
$ command -v xzless
/home/linuxbrew/.linuxbrew/bin/xzless
$ xzless --version
xzless (XZ Utils) 5.8.4
On this machine, the interactive shell finds a Linuxbrew build first. The distro copy is /usr/bin/xzless and reports 5.4.5. Both provide the same documented interface for this guide, but checking matters when debugging a script or comparing output between hosts. Use the absolute path if you need the distro copy:
$ /usr/bin/xzless --version
xzless (XZ Utils) 5.4.5
Checkpoint
Record the path and version before reporting a version-specific problem. Do not infer the package version from the command name alone.
2. Open one compressed text file
Pass the archive as an ordinary argument:
$ xzless /var/log/example.log.xz
xzless sends the uncompressed text to less. The pager opens interactively, so the command may appear to wait until you quit. Press q to return to the shell. Use the usual less controls: Space moves forward a page, b moves back, and /pattern searches forwards. Press n for the next match. The exact control set belongs to your installed less; consult man less if a key behaves differently.
The input is treated as text. Avoid using this command as a way to inspect arbitrary binary archives: decompressed binary data can contain control characters that make a terminal unpleasant or misleading to use. If a file came from an untrusted source, prefer a controlled output destination or inspect it with a tool designed for that file type.
3. Open several files and keep their names visible
Give multiple archive paths when you want to move between related files:
$ xzless /var/log/example.log.2.xz /var/log/example.log.1.xz
less treats these as separate input files. Its commands can move between files, while the status line identifies the current one. Quote paths containing spaces or shell metacharacters:
$ xzless -- "$HOME/archive notes/meeting.log.xz"
The -- marks the end of options. It prevents a file name beginning with a hyphen from being interpreted as a less option. It does not make an untrusted path safe in every situation, so quote the path as shown and avoid evaluating it as shell code.
4. Stream compressed input through standard input
If the data is already in a pipeline, omit the file argument:
$ xz -cd -- /var/log/example.log.xz | xzless
sample log lines appear in less
This is useful when another command has selected the archive or when you want to combine decompression with a preprocessing step. A simpler direct invocation is normally easier to audit. With no file argument, the manpage defines xzless as reading standard input.
Do not assume that a pipeline's exit status describes the whole operation. A decompressor can fail before the pager exits, and ordinary shell pipelines usually report only the last command's status. If a script must detect decompression errors, handle that explicitly in the shell and test the archive separately before presenting it.
5. Use lzless when compatibility requires it
lzless is the compatibility name provided for users of the older LZMA Utils naming. It accepts the same file and standard-input forms:
$ lzless /srv/archive/config.txt.lzma
$ lzless --version
xzless (XZ Utils) 5.4.5
The alias does not mean that the file must use an older toolchain. The installed command supports xz and lzma compressed text, and the alias is retained for compatibility. Prefer xzless in new documentation unless an existing script or operator habit specifically uses lzless.
6. Understand the environment boundary
xzless sets LESSOPEN so that less invokes the xz decompressor for input files. It also sets LESSMETACHARS unless that variable is already present. These are implementation details that explain why the command can hand compressed files directly to less.
The command's pager choice is not configurable through an environment variable in the way xzmore's choice is. If you need different pager behaviour, use options accepted by your installed less, for example:
$ xzless -N /var/log/example.log.xz
Here -N is a less option that displays line numbers. Verify options against man less on the target host rather than assuming that every version has the same defaults.
Done means
- You checked the resolved command and version when reproducibility mattered.
- You viewed an xz or lzma text file without extracting it.
- You know
qexits the pager and/patternsearches it. - You can pass several files or omit the file argument for standard input.
- You treated
lzlessas a compatibility alias and kept pager options tied toless.