Grepping within a gzipped tarball (BASH)

You've got a (presumably) large tarball that you want to grep within, but don't want to extract the thing to do so

What you want to know, is which files within the archive contain a specific pattern so that you can extract just those one

This snippet details how to run grep against files within a tarball

Details

  • Language: BASH

Snippet

tar xzf [tarball] --to-command 'grep -H --label="$TAR_FILENAME" [grep options] '[pattern]'; true'

Usage Example

# Build a tarball to search
echo abcdedfg | tee not-a.txt
fortune | cowsay | tee not-b.txt
echo abcdefg0123456aa |  tee not-c.txt
echo 01221665889463 |  tee is-d.txt
echo 0118 999 881 999 119 725 3 | tee is-e.txt
tar czf files.tar.gz *txt

# Run the grep - anything that has lines starting with numbers
tar xzf files.tar.gz --to-command 'grep -H --label="$TAR_FILENAME" -P '^[0-9]+'; true'
is-d.txt:01221665889463
is-e.txt:0118 999 881 999 119 725 3