How to List or Search for Files in a .tar / .tar.gz Archive – CloudSavvy IT

Bash Shell

If you have a huge tar backup file and you need to grab some files out of it, wouldn’t it be easier if you can look inside first? Luckily there’s an easy option to do so.

Simply use the -t option to list the contents of the archive because that letter says “list” to everybody and it’s so easy to remember. Yeah. Combine with the -f option to specify the tarfile that you’re getting the list from.

tar -tf tarfile

Of course just using that option will list the entire contents of the archive file, which is gonna be a lot. You can always pipe that through grep to find what you are looking for, but you can also pass a file name as an argument.

tar -tf tarfile filename

Alternatively you can use the wild cards argument to search through the archive for everything matching a pattern. For instance, to find every PNG image in an archive you can use the following command:

tar -tf tarfile --wildcards '*.png'

As mentioned before, you could also use grep to accomplish the same thing, of course. The difference is that the wildcard command is a little more efficient at the cost of having less power.

tar -tf tarfile | grep ".png"

And thus ends the story of how a single letter command line argument turned into an article that you read.