10

Is there a quick way to check if a gzipped file is empty, or do I have to unzip it first?

example:

$ touch foo
$ if [ -s foo ]; then echo not empty; fi
$ gzip foo
$ if [ -s foo.gz ]; then echo not empty; fi
not empty
$ wc -l foo.gz
      1 foo.gz

5 Answers 5

11

gzip -l foo.gz | awk 'NR==2 {print $2}' prints the size of the uncompressed data.

if LC_ALL=C gzip -l foo.gz | awk 'NR==2 {exit($2!=0)}'; then
  echo foo is empty
else
  echo foo is not empty
fi

Alternatively you can start uncompressing the data.

if [ -n "$(gunzip <foo.gz | head -c 1 | tr '\0\n' __)" ]; then
    echo "foo is not empty"
else
    echo "foo is empty"
fi

(If your system doesn't have head -c to extract the first byte, use head -n 1 to extract the first line instead.)

4
  • I assume LC_ALL=C is there to ensure that gzip does not put in thousand separators in numbers so the field can be compared to zero?
    – camh
    Commented Feb 1, 2011 at 20:36
  • 1
    @camh: It's more general paranoia when parsing formatted output from a command. It could be the number format, or that in some language there are two header lines, or many other things I just haven't thought of. In the case of gzip, I think nothing bad happens, but LC_ALL=C can't hurt. Commented Feb 1, 2011 at 23:44
  • 1
    The second alternative will fail if the file has data but does not have a newline; it also won't print the line as read is being invoked in a subshell (and $line is not propagated to the parent).
    – Chris Down
    Commented Sep 8, 2012 at 0:15
  • 1
    @ChrisDown Well spotted. Your fix isn't enough though (plus the way you wrote it is bash-only). If the file starts with a null byte, the shell (other than zsh) will see an empty string when it shouldn't. A pipe through tr fixes that. Commented Sep 8, 2012 at 11:44
4

If by 'empty' you mean that the uncompressed file is 0 bytes, you could use gzip --list foo.gz to determine the size of the uncompressed file, it would require some parsing to automate it. It looks something like this:

$ gzip --list foo.gz
         compressed        uncompressed  ratio uncompressed_name
                 24                   0   0.0% foo
0
2
test -z $(gzip -cd foo.gz | head -c1) && echo "empty"

Or with if:

if [ -z $(gzip -cd foo.gz | head -c1) ]; then
  echo "empty"
fi

zcat is sometimes linked to gunzip -c or gzip -cd, if you want to use it as the shorter "form".

0
0

Please note that the gzip file format only allows 32 bits for storing the original file size, so the number there is the size modulo 2^32. Hence the size given by "gzip -l" is not a definitive test for emptiness.

1
  • 2
    Please make this a more complete answer by including an example of how you would approach a solution.
    – George M
    Commented Jul 5, 2012 at 13:01
0

If the gzip file size is 51, it is empty. Whenever I added a char in a file and gzip the file, zipped file size begins to increase from 51.

However you can guarantee it with zcat. in gzip man:

The uncompressed size is given as -1 for files not in gzip format, such as compressed .Z files. To get the uncompressed size for such a file, you can use:

zcat file.Z | wc -c

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .