X
Popular Searches

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

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.

Lowell Heddings Lowell Heddings
Lowell is the founder and CEO of LifeSavvy Media. He’s been running the show since creating How-To Geek back in 2006. Over the last decade, Lowell has personally written more than 1000 articles which have been viewed by over 250 million people. Prior to starting How-To Geek, Lowell spent 15 years working in IT doing consulting, cybersecurity, database management, and programming work. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.