Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a large tarball that is busy being FTP'd over from a remote system to our local system.

I want to know if it is possible to start untarring lets say 50 files at a time so that those files can begin being processed while the transfer takes place.

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

Here is a detailed explanation on how it is possible to extract specific files from an archive. Specifically GNU tar can be used to extract a single or more files from a tarball. To extract specific archive members, give their exact member names as arguments.

For example:

tar --extract --file={tarball.tar} {file}

You can also extract those files that match a specific globbing pattern (wildcards). For example, to extract from cbz.tar all files that begin with pic, no matter their directory prefix, you could type:

tar -xf cbz.tar --wildcards --no-anchored 'pic*'

To extract all php files, enter:

tar -xf cbz.tar --wildcards --no-anchored '*.php'

Where,

-x: instructs tar to extract files.
-f: specifies filename / tarball name.
-v: Verbose (show progress while extracting files).
-j: filter archive through bzip2, use to decompress .bz2 files.
-z: filter archive through gzip, use to decompress .gz files.
--wildcards: instructs tar to treat command line arguments as globbing patterns.
--no-anchored: informs it that the patterns apply to member names after any / delimiter.

share|improve this answer
2  
I don't want to extract specific files. I just want to extract the first 50 files as I don't know what the names of the files are. –  Pieter van Niekerk Jul 3 '12 at 6:35
3  
You can get a list of the filenames using "tar -tf", take the first 50 using "head", then feed that list into another tar command as the list of filenames to extract. Like this: "tar -xf file.tar --no-anchored `tar -tf file.tar|head -50`" –  Simon Hibbs Jul 3 '12 at 9:01
    
It is quite possible (in my testing) to extract a partially trandferred 50th file.. It would be a good idea to avoid extracting the (current) last file in the -t list until the tarball is fully downloaded. At any point of time, the list shows only filenames which have been, or are being, transferred, ie. not the full list; until it is fully downloaded. –  Peter.O Jul 4 '12 at 3:36
add comment

Didn't try this myself, but how about this:

tar xvf archive.tar | head -n50

Tar outputs a line to STDOUT for each file extracted, then the head command will kill the pipe after 50 lines. Upon the pipe dying, I'd expect the tar to die too.

share|improve this answer
    
Sorry, that should of course been a head. Let me change my answer. –  jippie Jul 3 '12 at 21:36
    
I haven't checked, but I fear that due to the pipe buffer, tar may extract more than 50 files before it's SIGPIPEd, and in particular it may extract the last, incomplete file. –  Gilles Jul 3 '12 at 22:18
add comment

If you need to process the files as they arrive, then tar(1) isn't the right tool for the job. Cobble up a web service that gets the file and hands it over for frobbing?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.