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

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.


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.

  • 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. – pauljohn32 Apr 22 '17 at 4:27
  • 1
    This command actually uncompress the archive on the air. – Ravexina Apr 22 '17 at 4:57
  • @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 Apr 22 '17 at 5:13
  • @Ravexina: That is correct and it can't be done in any other way because the .tar file holds the file metadata. – NZD Apr 22 '17 at 5:28
  • @NZD , Yeah, However I think OP is looking for someway to do it without uncompressing the file ;) – Ravexina Apr 22 '17 at 5:39

I'm finding everything sites in the web, and don't resolve this problem the get size when file size is bigger of 4GB.

first, which is most faster?

[oracle@base tmp]$ time zcat oracle.20180303.030001.dmp.tar.gz | wc -c

    real    0m45.761s
    user    0m43.203s
    sys     0m5.185s
[oracle@base tmp]$ time gzip -dc oracle.20180303.030001.dmp.tar.gz | wc -c

    real    0m45.335s
    user    0m42.781s
    sys     0m5.153s
[oracle@base tmp]$ time tar -tvf oracle.20180303.030001.dmp.tar.gz
    -rw-r--r-- oracle/oinstall 111828 2018-03-03 03:05 oracle.20180303.030001.log
    -rw-r----- oracle/oinstall 6666911744 2018-03-03 03:05 oracle.20180303.030001.dmp

    real    0m46.669s
    user    0m44.347s
    sys     0m4.981s

definitely, tar -xvf is the most faster, but ¿how to cancel executions after get header?

my solution is this:

[oracle@base tmp]$  time echo $(timeout --signal=SIGINT 1s tar -tvf oracle.20180303.030001.dmp.tar.gz | awk '{print $3}') | grep -o '[[:digit:]]*' | awk '{ sum += $1 } END { print sum }'

real    0m1.029s
user    0m0.012s
sys     0m0.063s
  • @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 – RaZieRSarE Mar 12 '18 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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