Although bzip2 is a block-based compression algorithm, so theoretically you could just find the specific blocks you want to unpack, it would be difficult (for example, if the last ten lines that you ultimately want to see actually span two or more compressed blocks?).
To answer your next question, you can do it that actually decompresses the entire file, so itβs wasteful in a way, but it doesnβt try to store this file anywhere, so you donβt run the storage capacity questions:
bzcat file.bz2 | head -10 bzcat file.bz2 | tail -10
If your distribution does not include bzcat (which would be a little unusual in my experience), bzcat equivalent to bzip2 -d -c .
However, if your ultimate goal is to compare two compressed files (which could be compressed at different levels, and therefore comparing the actual compressed files directly does not work), you can do this (assuming bash as your shell):
cmp <(bzcat file1.bz2) <(bzcat file2.bz2)
This will decompress both files and compare the uncompressed data byte by byte without saving any of the decompressed files anywhere.
source share