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.