How to check if a Unix.tar.gz file is a valid file without decompression?

I also found this link . But I was wondering if there is a ready-made command line solution?

+68
validation gzip tar gunzip
Jan 04 '10 at 19:46
source share
8 answers

How about just getting a tarball list and throwing out the output rather than unpacking the file?

tar -tzf my_tar.tar.gz >/dev/null 

Edited in accordance with the comment. Thanks zrajm!

Change as per comment. Thanks Frozen Flame! This test in no way implies data integrity. Since it was designed as a utility for tape archiving, most tar implementations will have multiple copies of the same file!

+85
Jan 04 '10 at 19:51
source share

perhaps you can use the gzip -t option to check file integrity

http://linux.about.com/od/commands/l/blcmdl1_gzip.htm

from: http://unix.ittoolbox.com/groups/technical-functional/shellscript-l/how-to-test-file-integrity-of-targz-1138880

To verify that the gzip file is not corrupted:

 gunzip -t file.tar.gz 

To check the tar file inside, it is not corrupted:

 gunzip -c file.tar.gz | tar t > /dev/null 

As part of the backup, you can probably just run the last command and check the value of $? then for the value 0 (success). If either resin or gzip has a problem, $? will have a nonzero value.

+77
Jan 04 '10 at 19:51
source share

If you want to make a real test extract of the tar file without extracting it to disk, use the -O option. This selects the extract to standard output instead of the file system. If the tar file is damaged, the process will be aborted with an error.

An example of a failed ball test ...

 $ echo "this will not pass the test" > hello.tgz $ tar -xvzf hello.tgz -O > /dev/null gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error exit delayed from previous errors $ rm hello.* 

Working example ...

 $ ls hello* ls: hello*: No such file or directory $ echo "hello1" > hello1.txt $ echo "hello2" > hello2.txt $ tar -cvzf hello.tgz hello[12].txt hello1.txt hello2.txt $ rm hello[12].txt $ ls hello* hello.tgz $ tar -xvzf hello.tgz -O hello1.txt hello1 hello2.txt hello2 $ ls hello* hello.tgz $ tar -xvzf hello.tgz hello1.txt hello2.txt $ ls hello* hello1.txt hello2.txt hello.tgz $ rm hello* 
+24
Jan 17 '12 at 19:41
source share

You can also check the contents of the * .tag.gz file using pigz (parallel gzip) to speed up archive checking:

 pigz -cvdp number_of_threads /[...]path[...]/archive_name.tar.gz | tar -tv > /dev/null 
+9
Dec 09
source share

I tried the following command and they work well.

 bzip2 -t file.bz2 gunzip -t file.gz 

However, we can find that these two teams take a lot of time. Perhaps we need another quick way to identify intact compression files.

+2
Feb 18 '16 at 21:32
source share

A good option is to use tar -tvvf <filePath> , which adds a line that tells the file type.

Example in a valid .tar file:

 > tar -tvvf filename.tar drwxr-xr-x 0 diegoreymendez staff 0 Jul 31 12:46 ./testfolder2/ -rw-r--r-- 0 diegoreymendez staff 82 Jul 31 12:46 ./testfolder2/._.DS_Store -rw-r--r-- 0 diegoreymendez staff 6148 Jul 31 12:46 ./testfolder2/.DS_Store drwxr-xr-x 0 diegoreymendez staff 0 Jul 31 12:42 ./testfolder2/testfolder/ -rw-r--r-- 0 diegoreymendez staff 82 Jul 31 12:42 ./testfolder2/testfolder/._.DS_Store -rw-r--r-- 0 diegoreymendez staff 6148 Jul 31 12:42 ./testfolder2/testfolder/.DS_Store -rw-r--r-- 0 diegoreymendez staff 325377 Jul 5 09:50 ./testfolder2/testfolder/Scala.pages Archive Format: POSIX ustar format, Compression: none 

Corrupted .tar file:

 > tar -tvvf corrupted.tar tar: Unrecognized archive format Archive Format: (null), Compression: none tar: Error exit delayed from previous errors. 
+1
Aug 01 '13 at 17:29
source share

> use the -O option. [...] If the tar file is damaged, the process will be aborted with an error.

Sometimes yes, but sometimes no. Consider the example of a damaged file:

 echo Pete > my_name tar -cf my_data.tar my_name # // Simulate a corruption sed < my_data.tar 's/Pete/Fool/' > my_data_now.tar # // "my_data_now.tar" is the corrupted file tar -xvf my_data_now.tar -O 

He shows:

 my_name Fool 

Even if you follow

 echo $? 

tar said there was no error:

 0 

but the file was damaged, now it is "Fool" instead of "Pete".

0
Oct 16 '12 at 15:58
source share

These are all very suboptimal solutions. From the GZIP specification

ID2 (identification 2)
They have fixed values ​​ID1 = 31 (0x1f, \ 037), ID2 = 139 (0x8b, \ 213) to determine the file as being in gzip format.

Must be encoded into any language that you use.

0
May 12 '17 at 23:18
source share



All Articles