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

- 333,661
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...

- 31,260
gzip
will scan the entire file or just the header? The man page does not clarify that. – Leo Aug 30 '20 at 04:50gzip -t
will scan the entire file. If you just want to test whether or not the file is compressed usegzip --list
(redirect errors if you want) and check$?
– Leo Aug 30 '20 at 04:56The 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 ofgzip
, but as for me, I've tested it with a seemingly incompletegzip-compressed
tarball, and it returned not only theexit code
but also theunexpected end of file
error message tostdout
, which is the same generic error message that's also returned with other unrelated file formats, and corrupted compressed archives. – xquilt Aug 19 '22 at 00:23