[Tar.gz] A little help from Awk to calculate space an archive will take without extracting it!

Ran into a situation today where there was limited space on a server, about 4GB free. I had a good feeling that the tar.gz file that needed to be extracted would exceed the available space. So, I came up with a really quick way to calculate the uncompressed archive without actually uncompressing it. A simple one liner can do the trick:

swinful>  gunzip < largefile.tar.gz | \
tar tvf – | \
awk ‘{ sum += $3}; END {print sum/1024} ‘

That will printout the size in GB for  Solaris and Linux systems. AIX will output the total in kilobytes and FreeBSD will output the total as zero! This is because in FreeBSD the file size is in the 5th column. So, if you are on BSD replace “$3” with “$5”.

Enjoy,
swinful

Advertisements
This entry was posted in *Nix. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s