41

How can I check the validity of a gz file, I do not have the hash of the file, I'm using gzip -t but it is not returning any output.

2 Answers 2

59

The gzip -t command only returns an exit code to the shell saying whether the file passed the integrity test or not.

Example (in a script):

if gzip -t file.gz; then
    echo 'file is ok'
else 
    echo 'file is corrupt'
fi

Adding -v will make it actually report the result with a message.

Example:

$ gzip -v -t file.gz
file.gz:        OK

So the file is ok. Let's corrupt the file (by writing the character 0 at byte 40 in the file) and try again.

$ dd seek=40 bs=1 count=1 of=file.gz <<<"0"
1+0 records in
1+0 records out
1 bytes transferred in 0.000 secs (2028 bytes/sec)
$ gzip -v -t file.gz
file.gz:        gzip: file.gz: Inappropriate file type or format

The integrity of a file with respect to its compression does not guarantee that the file contents is what you believe it is. If you have an MD5 checksum (or some similar checksum) of the file from whomever provided it, then you would be able to get an additional confirmation that the file not only is a valid gzip archive, but also that its contents is what you expect it to be.

3
  • Does someone know if gzip will scan the entire file or just the header? The man page does not clarify that.
    – Leo
    Aug 30, 2020 at 4:50
  • 1
    From my experimentation gzip -t will scan the entire file. If you just want to test whether or not the file is compressed use gzip --list (redirect errors if you want) and check $?
    – Leo
    Aug 30, 2020 at 4:56
  • The gzip -t command only returns an exit code to the shell saying whether the file passed the integrity test or not. Maybe with older versions of gzip, but as for me, I've tested it with a seemingly incomplete gzip-compressed tarball, and it returned not only the exit code but also the unexpected end of file error message to stdout, which is the same generic error message that's also returned with other unrelated file formats, and corrupted compressed archives.
    – xquilt
    Aug 19, 2022 at 0:23
5

gzip -t doesn't have any output, other than the return code, if it's a correct gzip compressed file.

It only returns an error if you're trying it on something that isn't a gzip compressed file:

steamsrv@leviathan:~$ gzip -t commands.txt

gzip: commands.txt: not in gzip format

Conclusion: Your file is almost certainly a gzip compressed file. What I can't tell you is whether it's the exact file that you think it's supposed to be, which is what the hash would be useful for...

You must log in to answer this question.

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