There are a few ways to extract a single file from a .tar / .tar.gz archive. The easiest way is to use the tar command. The tar command can extract the files and folders in the archive, as well as any files that were added by the archive’s creator. To extract a single file from a .tar / .tar.gz archive, use the following command: tar -xvf filename_to_extract
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:
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.
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:
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:
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:
Hopefully you learned something today.