Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
add comment

4 Answers

up vote 6 down vote accepted

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.)

share|improve this answer
    
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 Feb 1 '11 at 20:36
    
@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. –  Gilles Feb 1 '11 at 23:44
    
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 Sep 8 '12 at 0:15
    
@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. –  Gilles Sep 8 '12 at 11:44
add comment
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".

share|improve this answer
2  
I'd hate to accidentally use that on a multi-gigabyte .gz file. Maybe you ought to tack on a | head -c1 or something. –  Jander Feb 1 '11 at 16:18
    
Good point @Jander ! –  wag Feb 1 '11 at 20:54
add comment

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
share|improve this answer
    
This is essentially answer 1! –  Henno Brandsma Feb 2 '11 at 9:14
    
... which was posted after this one. –  jsbillings Feb 2 '11 at 15:45
add comment

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.

share|improve this answer
1  
Please make this a more complete answer by including an example of how you would approach a solution. –  uther Jul 5 '12 at 13:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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