Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a 32GB .tar.gz archive and I'd like to know the size of the files if I unpack this compressed archive. I'd like to avoid unpacking the archive first and than use e.g. du.

Is it also possible to find out the size of the contained files without unpacking the compressed archive (on a Linux and/or MacOSX system)?

For another archive I know, that it also contains .tar.gz files. Is it also possible to calculate the size of the unpacked archives that are contained within an archive? (for example by setting a level to which the "unpacking" should be simulated?)

share|improve this question

migrated from stackoverflow.com Dec 19 '12 at 16:35

This question came from our site for professional and enthusiast programmers.

    
What kind of system/terminal/command processor? Linux, Windows, ... (bash, ...)? –  Beachwalker Dec 19 '12 at 9:57
    
I'm using the bash on Linux and MacOSX –  Sven Dec 19 '12 at 10:17

2 Answers 2

up vote 3 down vote accepted

Sure. Just use -tv to list the contents with their sizes. E.g.

% tar -tvzf sometools2.tar.gz 
-rw-r--r-- madler/admin   3442 2005-02-27 21:40 pngdat.c
-rw-r--r-- madler/admin  24938 2005-02-27 21:39 infgen.c

If you want to add up the sizes (like du), you can use awk:

% tar -tvzf sometools2.tar.gz|awk '{ s += $3 } END { print s }'
28380

For an imbedded .tar.gz file, you would need to do those individually when you find them by sending them to stdout with -O:

% tar -tvzf imbed.tar.gz 
-rw-r--r-- madler/staff    505 2012-02-12 00:06 lucas.c
-rw-r--r-- madler/staff  27913 2005-03-20 11:10 lzwtry.c
-rw-r--r-- madler/staff   8314 2005-02-27 21:42 sometools2.tar.gz
% tar -xOzf imbed.tar.gz sometools2.tar.gz | tar -tvzf - | awk '{ s += $3 } END { print s }'
28380

You can write a script to find those in the -tv output and then extract them, and even do it recursively. I will leave that as an exercise for the reader.

Note, these options are for GNU tar, which is what is on both Linux and Mac OS X. The options for BSD tar may be different.

share|improve this answer
    
+1 cool... didn't knew that option –  Beachwalker Dec 19 '12 at 15:45

I don't know how to do something like this on the terminal (AFAIK it is not possible). But most programming libs for extracting archives also allow to query content information (e.g. tree, size of content) without extracting the whole compressed contents.

So you could create a command line tool with any programming language that will fit your needs and then call it from the command line.

share|improve this answer

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.