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

后端 未结 8 1533

I have found the question How to determine if data is valid tar file without a file?, but I was wondering: is there a ready made command line solution?

8条回答
  •  渐次进展
    2020-11-30 20:32

    If you want to do a real test extract of a tar file without extracting to disk, use the -O option. This spews the extract to standard output instead of the filesystem. If the tar file is corrupt, the process will abort with an error.

    Example of failed tar 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*
    

提交回复
热议问题