Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the total content size of a tar gz file

Tags:

gzip

tar

How can I extract the size of the total uncompressed file data in a .tar.gz file from command line?

like image 878
Ztyx Avatar asked Apr 26 '10 08:04

Ztyx


People also ask

How do I check the size of a gz file in Linux?

For some applications it is useful to determine the uncompressed size of a file that has been compressed by the gzip algorithm. From the command line this can be done by using the -l option of the gzip program.

How do I view the contents of a gz file in Unix?

If you need to check the contents of a compressed text file on Linux, you don't have to uncompress it first. Instead, you can use a zcat or bzcat command to extract and display file contents while leaving the file intact. The "cat" in each command name tells you that the command's purpose is to display content.


2 Answers

This works for any file size:

zcat archive.tar.gz | wc -c

For files smaller than 4Gb you could also use the -l option with gzip:

$ gzip -l compressed.tar.gz
     compressed        uncompressed  ratio uncompressed_name
            132               10240  99.1% compressed.tar
like image 150
Matthew Mott Avatar answered Oct 16 '22 14:10

Matthew Mott


This will sum the total content size of the extracted files:

$ tar tzvf archive.tar.gz | sed 's/ \+/ /g' | cut -f3 -d' ' | sed '2,$s/^/+ /' | paste -sd' ' | bc

The output is given in bytes.

Explanation: tar tzvf lists the files in the archive in verbose format like ls -l. sed and cut isolate the file size field. The second sed puts a + in front of every size except the first and paste concatenates them, giving a sum expression that is then evaluated by bc.

Note that this doesn't include metadata, so the disk space taken up by the files when you extract them is going to be larger - potentially many times larger if you have a lot of very small files.

like image 43
Ztyx Avatar answered Oct 16 '22 13:10

Ztyx