How to check if a file contains only zeros in a Linux shell?

后端 未结 6 2081
隐瞒了意图╮
隐瞒了意图╮ 2021-01-04 07:36

How to check if a large file contains only zero bytes (\'\\0\') in Linux using a shell command? I can write a small program for this but this seems to be an ov

6条回答
  •  耶瑟儿~
    2021-01-04 07:54

    If you have Bash,

    cmp file <(tr -dc '\000' 

    If you don't have Bash, the following should be POSIX (but I guess there may be legacy versions of cmp which are not comfortable with reading standard input):

    tr -dc '\000' 

    Perhaps more economically, assuming your grep can read arbitrary binary data,

    tr -d '\000' 

    I suppose you could tweak the last example even further with a dd pipe to truncate any output from tr after one block of data (in case there are very long sequences without newlines), or even down to one byte. Or maybe just force there to be newlines.

    tr -d '\000' 

提交回复
热议问题