0

we want to check the tar.gz files if they are corrupted

for example

I use the file command

file  spark2-hdp-yarn-archive.tar.gz
spark2-hdp-yarn-archive.tar.gz: gzip compressed data, was "spark2-hdp-yarn-archive.tar", last modified: Wed Aug  1 15:05:27 2018, max compression

in this case seems that file is ok

but I am not sure if file command is the right approach to check the compressed file

second what are the expected results that indicate the tar.gz file is corrupted ?

Judy
  • 1,595
  • 6
  • 19
  • 41
  • `file` just checks the first few bytes of the file to determine the format, it doesn't verify the file. The only way to do it would be to try to decompress it and see if you get an error. – Barmar Sep 04 '18 at 16:13
  • 1
    [so] is for programming questions, not questions about using or configuring Linux and its applications. [su] or [unix.se] would be better places for questions like this. – Barmar Sep 04 '18 at 16:13
  • List the table of contents with `tar tzvf spark2-hdp-yarn-archive.tar.gz` – Basile Starynkevitch Sep 04 '18 at 16:39
  • what is the value to check , do you mean like echo $? , and if this diff from zero then this is corrupted file ? – Judy Sep 04 '18 at 16:43

2 Answers2

4

Use this :

arch=spark2-hdp-yarn-archive.tar.gz
if gzip -t "$arch" &>/dev/null; then
    echo ok
else
    echo >&2 "File corrupted"
    exit 1
fi
  • 1
    Please avoid answering [off-topic](https://stackoverflow.com/help/on-topic) fodder. Instead, close it and move on. It is sometimes helpful to suggest a site where the question may be on-topic. – jww Sep 05 '18 at 00:17
1

The gzip tool allows you to test an archive. You would enter a command like

gzip -t spark2-hdp-yarn-archive.tar.gz

According to the man pages the -t flag tests the integrity of the file. There may be many errors you receive when testing the file, but the one I see most commonly for corrupted archives is

gzip: blah.gz: unexpected end of file

(With blah.gz being the file examined).

MrW
  • 310
  • 2
  • 7
  • can we use the "echo $?" in order to check it? – Judy Sep 04 '18 at 16:20
  • can we also do gunzip -c file.tar.gz | tar t > /dev/null , and then check the $? – Judy Sep 04 '18 at 16:24
  • Please avoid answering [off-topic](https://stackoverflow.com/help/on-topic) fodder. Instead, close it and move on. It is sometimes helpful to suggest a site where the question may be on-topic. – jww Sep 05 '18 at 00:17