Bash Shell

If you’ve ever run into a scenario where you have a huge tar backup file but you only need to extract a single file, you’re reading the right article today. Why bother extracting the whole thing just to grab that one file?

Instead, you just need to know the syntax for extracting a single file out of that archive. And here is that syntax:

tar --extract --file=<tarfile> <path/to/singlefile>

So, for example, if you want to extract xmlrpc.php from a downloaded WordPress archive, you’d use the following, since everything inside of the wordpress tar file is in a “wordpress” folder.

tar --extract --file=latest.tar.gz wordpress/xmlrpc.php

This, of course, is using the verbose syntax. Instead of using --extract you can use -x, and instead of using --file you can use -f, and you can even put them both together into a single argument like -xf instead. (Note: Historically you’d also need the -z option to run through gzip, but in my testing it works fine without it).  So the shorter command would be:

tar -xf latest.tar.gz wordpress/xmlrpc.php

You can also extract a single folder using the same syntax. For example, to grab the entire wordpress/wp-includes folder from inside of the WordPress archive, you’d simply do:

tar -xf latest.tar.gz wordpress/wp-includes

There’s also a --wildcard parameter that you can use to pull all files matching a pattern — for instance, if you wanted to grab all PNG images from an archive, you could do something like this:

tar -xf <tarball> --wildcards '*.png'

Hopefully you learned something today.

Profile Photo for Lowell Heddings Lowell Heddings
Lowell is the founder and CEO of How-To Geek. He’s been running the show since creating the site 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 »