Skip to main content
Nick Marsceau

Useful TAR Commands

A few weeks ago, I needed to pull a handful of files out of a very large tarball. Not knowing any other way to do that, I started fully extracting the archive. After 15 minutes, over 20,000 files had been extracted, and it was only about 10% done. I knew I was going to need to find a more efficient way to achieve what I wanted. After a few quick searches, I found several options for interacting with tarballs while they are still tarred or even zipped.

List the Contents of a Tarball


tar tf archive.tar

You can grep the output to search for a specific filename or all files with a certain extension.


tar tf archive.tar | grep -E '\.jpg$|\.webp$'

Search Within a Tarball


tar tf archive.tar | while read -r FILE;
do
    MATCHES=$(tar xf archive.tar "$FILE" -O | grep "the pattern to match")
    if test -n "$MATCHES"; then
        echo "$FILE:"$'\n'"$MATCHES"
    fi;
done

If the contents of a file within the tarball matches the pattern you search for, this will print out its internal path within the tarball followed by the grep output. This is based on the accepted answer for this Stack Overflow.

Given the length of this snippet, it might be a good candidate to set up as a custom function in your shell profile's initialization script.

Extract a Single File From a Tarball


tar xf archive.tar internal/path

The internal path specified must exactly match the full path to the file within the tarball (as seen in the output of tar tf archive.tar).

Interact With Zipped Tarballs

To perform any of these operations on a gzipped tarball, just add the z flag.


tar tzf archive.tar.gz                  # List the contents
tar xzf archive.tar.gz internal/path    # Extract a single file
# etc.

As far as I know, the j option will allow tar to operate on bzipped tarballs.

Putting It All Together

One line to extract all the webp images from a gzipped tarball:


tar tzf archive.tar.gz | grep "\.webp" | xargs tar xzf archive.tar.gz