und*_*ack 99 validation gzip tar gunzip
我发现了如何确定数据是否是没有文件的有效tar文件的问题?,但我想知道:是否有现成的命令行解决方案?
Rob*_*lls 112
如何获取tarball列表并丢弃输出,而不是解压缩文件?
tar -tzf my_tar.tar.gz >/dev/null
Run Code Online (Sandbox Code Playgroud)
根据评论编辑.谢谢zrajm!
根据评论进行编辑.谢谢冷冻火焰!该测试决不意味着数据的完整性.因为它被设计为磁带归档实用程序,所以tar的大多数实现都允许同一文件的多个副本!
Joh*_*ker 92
您可以使用gzip -t选项来测试文件的完整性
http://linux.about.com/od/commands/l/blcmdl1_gzip.htm
来自:http://unix.ittoolbox.com/groups/technical-functional/shellscript-l/how-to-test-file-integrity-of-targz-1138880
测试gzip文件没有损坏:
gunzip -t file.tar.gz
Run Code Online (Sandbox Code Playgroud)
测试里面的tar文件没有损坏:
gunzip -c file.tar.gz | tar t > /dev/null
Run Code Online (Sandbox Code Playgroud)
作为备份的一部分,您可能只需运行后一个命令并检查$的值?之后获得0(成功)值.如果tar或gzip有问题,$?将具有非零值.
小智 30
如果要在不提取到磁盘的情况下对tar文件进行实际测试提取,请使用-O选项.这会将数据提取到标准输出而不是文件系统.如果tar文件已损坏,则进程将中止并显示错误.
失败的焦油球测试示例......
$ 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.*
Run Code Online (Sandbox Code Playgroud)
工作实例......
$ 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*
Run Code Online (Sandbox Code Playgroud)
小智 11
您还可以使用pigz(parallel gzip)检查*.tag.gz文件的内容以加快存档检查:
pigz -cvdp number_of_threads /[...]path[...]/archive_name.tar.gz | tar -tv > /dev/null
Run Code Online (Sandbox Code Playgroud)
一个不错的选择是使用tar -tvvf <filePath>它添加一行来报告文件类型。
有效 .tar 文件中的示例:
> 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
Run Code Online (Sandbox Code Playgroud)
损坏的 .tar 文件:
> tar -tvvf corrupted.tar
tar: Unrecognized archive format
Archive Format: (null), Compression: none
tar: Error exit delayed from previous errors.
Run Code Online (Sandbox Code Playgroud)