46

Is there a way to print the decompressed size of a .bz2 file without actually decompressing the entire thing?

endolith
  • 7,704

3 Answers3

48

As noted by others, bzip2 doesn't provide much information. But this technique works -- you will have to decompress the file, but you won't have to write the decompressed data to disk, which may be a "good enough" solution for you:

$ ls -l foo.bz2
-rw-r--r-- 1 ~quack ~quack 2364418 Jul  4 11:15 foo.bz2

$ bzcat foo.bz2 | wc -c         # bzcat decompresses to stdout, wc -c counts bytes
2928640                         # number of bytes of decompressed data

You can pipe that output into something else to give you a human-readable form:

$ ls -lh foo.bz2
-rw-r--r-- 1 quack quack 2.3M Jul  4 11:15 foo.bz2

$ bzcat foo.bz2 | wc -c | perl -lne 'printf("%.2fM\n", $_/1024/1024)'
2.79M
quack quixote
  • 43,504
1

I had to do this for a list of files, and altered @quack quixote's answer to have a loop.

for file in *.bz2; do
    bzcat ${file} | wc -c | perl -nle 'printf("%s: %.2fM\n", "'${file}'", $_/1024/1024)'
done
MikeB
  • 111
-3

To read .bz extension text file without unzipping.

bzcat dbtax_ext_en.ttl.bz2 |zless