How do I get a file size(original file size) within .tar.gz without uncompress it?

14,754

Solution 1

To get the uncompressed size of a ZIP file we can issue gzip with option --list or -l

gzip -l mytext.txt.tar.gz

This will give an output similar to this

gzip -l mytext.txt.tar.gz
         compressed        uncompressed  ratio uncompressed_name
               1475                4608  68.4% mytext.txt.tar

To have the compressed file size, the uncompressed size, and the compression ratio.

Solution 2

You can list the content (including original file sizes) of the tar file using:

tar -vtf myfile.tar.gz

If you only want myfile.txt:

tar -vtf myfile.tar.gz myfile.txt

This only works if you add the full file path, otherwise use:

tar -vtf myfile.tar.gz | grep myfile.txt

Note that tar will have to decompress the archive in order to get to the file information. It will however hide that from you.

If you specifically need a way to get to file meta-data without having to decompress the whole archive, you are better off using zip to store your files and directories. Zip uses a 'central directory' at the end of a zip-file that stores all file meta-data.

Share:
14,754

Related videos on Youtube

αғsнιη
Author by

αғsнιη

SeniorDevOpsEngineer at #HUAWEI since March-2015 (#opentowork https://www.linkedin.com/in/-rw-r--r--) ʷⁱˡˡⁱⁿᵍ ᵗᵒ ˢᵉᵉ ʸᵒᵘ ⁱⁿ ᵃ ᵐⁱʳʳᵒʳ ᵐᵃᵈᵉ ᵒᶠ ᵐʸ ᵉʸᵉˢ # touch 'you ◔◡◔'

Updated on September 18, 2022

Comments

  • αғsнιη
    αғsнιη over 1 year

    I make myfile.txt to zip file with below command, is there a way to get original file size 'myfile.txt' without unzipping it.

    tar -czf myfile.tar.gz myfile.txt
    
  • pauljohn32
    pauljohn32 about 7 years
    why don't we need the "z" in the command. It used to be necessary to signal that the gz should be ungzipped. "tar -tzvf fn.tar.gz", where t=test, z=unzip, v=verbose, f=filename.
  • Ravexina
    Ravexina about 7 years
    This command actually uncompress the archive on the air.
  • NZD
    NZD about 7 years
    @pauljohn32: tar is smart enough to detect the format, You only need it when creating an archive. See gnu.org/software/tar/manual/tar.html#SEC136
  • NZD
    NZD about 7 years
    @Ravexina: That is correct and it can't be done in any other way because the .tar file holds the file metadata.
  • Ravexina
    Ravexina about 7 years
    @NZD , Yeah, However I think OP is looking for someway to do it without uncompressing the file ;)
  • NZD
    NZD about 7 years
    @Ravexina: Thanks, update my answer with that.
  • RaZieRSarE
    RaZieRSarE about 6 years
    @dsstorefile I forgot to put execution time in the last command, which shows the significant reduction. and you can see the cancellation of decompression with the "timeout" command
  • Admin
    Admin almost 2 years
    If you are only interested in the uncompressed size of all files in a folder use: for i in $DIR/*; do gzip --list $i; done|grep -v uncomp|cut -b 25-40| paste -s -d+ - | bc