How do I `grep` through .xz compresses files?

6,111

If you look through the tooling provided by the xz RPM on RHEL/CentOS/Fedora distros this RPM includes a few helper wrapper scripts that you can enlist to make short work of this.

Identifying a Lead

With issues such as this I typically start by locating the RPMs that provide the tooling. In this case xz is the compression CLI so let's locate it and see what RPM provides it:

$ type -f xz
xz is /usr/bin/xz

$ rpm -qf /usr/bin/xz
xz-5.2.2-1.el7.x86_64

Now let's look and see if it provides anything with the name grep in it:

$ rpm -ql xz | grep -E 'bin/.*grep'
/usr/bin/xzegrep
/usr/bin/xzfgrep
/usr/bin/xzgrep

It does. So let's try using xzgrep since that's what we're after in terms of functionality:

$ xzgrep -l ocp-app-01c *
mom.log.4
vdsm.log.2.xz
vdsm.log.81.xz

NOTE: Above we're looking for occurrences of ocp-app-01c and printing files that contain it.

Usage

The usage is identical to grep:

$ xzgrep --help
Usage: xzgrep [OPTION]... [-e] PATTERN [FILE]...
Look for instances of PATTERN in the input FILEs, using their
uncompressed contents if they are compressed.

OPTIONs are the same as for 'grep'.

Report bugs to <[email protected]>.
Share:
6,111

Related videos on Youtube

slm
Author by

slm

Worked in the tech field for over 20+ years. Started out learning basic on an Apple IIe then on a TRS-80. Been interested in computer hardware and software my entire life. Consider myself lucky that my hobby as a kid/adult is what I get to do everyday earning a living. You can learn more about me here. ============================================================ Stolen from @Mokubai: First, please put down the chocolate-covered banana and step away from the European currency systems. You may consider how to ask a question.

Updated on September 18, 2022

Comments

  • slm
    slm over 1 year

    I'm using Red Hat Virtualization (RHV) and it logs all of its files in this directory:

    $ pwd
    /var/log/vdsm
    
    $ ls | column -c 80 | head -10
    backup          vdsm.log.34.xz      vdsm.log.69.xz
    import          vdsm.log.35.xz      vdsm.log.6.xz
    mom.log         vdsm.log.36.xz      vdsm.log.70.xz
    mom.log.1       vdsm.log.37.xz      vdsm.log.71.xz
    mom.log.2       vdsm.log.38.xz      vdsm.log.72.xz
    mom.log.3       vdsm.log.39.xz      vdsm.log.73.xz
    mom.log.4       vdsm.log.3.xz       vdsm.log.74.xz
    mom.log.5       vdsm.log.40.xz      vdsm.log.75.xz
    supervdsm.log   vdsm.log.41.xz      vdsm.log.76.xz
    upgrade.log     vdsm.log.42.xz      vdsm.log.77.xz
    

    I've used the z* tools such as zgrep & zcat to look through .gz & .Z files but it doesn't appear to deal with .xz compression.

    What's an easy way to grep through a directory of .xz files?

    • Jeff Schaller
      Jeff Schaller over 4 years
      Would it be fair to add a RHEL tag, since the OS (and answer) utilize RPM?
    • Stephen Kitt
      Stephen Kitt over 4 years
      I don’t think so, the conclusion of the answer is valid on other platforms too (the package providing xz in Debian has the same helpers). RPM isn’t RHEL-specific either.
    • slm
      slm over 4 years
      Same sentiment as @StephenKitt's.