Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract 1 file from tar.gz with bash

Tags:

linux

bash

gzip

tar

Is it possible to programmatically pull a single file from a decently sized .tar.gz without extracting the entire tarball to disk? Essentially I need to get inside large tar.gz files over the network and extract 1 small text file. It seems somewhat over-the-top to pull and extract the tarball to disk, then pull the file out, then delete everything else. Also I'm going to be doing this recursively (e.g. package dependencies, each text file points to more tar.gz's), so the less network traffic and cpu cycles I can get away with, the better.

like image 853
TJ L Avatar asked Aug 12 '10 16:08

TJ L


People also ask

How do I extract a single file from tar?

Now, if you want a single file or folder from the “tar” file, you need to use the name of the “tar” file and the path to a single file in it. So, we have used the “tar” command with the “-xvf” option, the name of the “tar” file, and the path of a file to be extracted from it as below.

How do I extract files from tar gz?

Simply right-click the item you want to compress, mouseover compress, and choose tar. gz. You can also right-click a tar. gz file, mouseover extract, and select an option to unpack the archive.


1 Answers

From the man page, to extract blah.txt from foo.tar.gz:

tar -xzf foo.tar.gz blah.txt

(And this goes on superuser, of course, but hey, prompt answers are nice too.)

like image 81
Cascabel Avatar answered Sep 30 '22 06:09

Cascabel