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

后端 未结 6 2053
隐瞒了意图╮
隐瞒了意图╮ 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:53

    If you're using bash, you can use read -n 1 to exit early if a non-NUL character has been found:

    <your_file tr -d '\0' | read -n 1 || echo "All zeroes."
    

    where you substitute the actual filename for your_file.

    0 讨论(0)
  • 2021-01-04 07:54

    The "file" /dev/zero returns a sequence of zero bytes on read, so a cmp file /dev/zero should give essentially what you want (reporting the first different byte just beyond the length of file).

    0 讨论(0)
  • 2021-01-04 07:54

    If you have Bash,

    cmp file <(tr -dc '\000' <file)
    

    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' <file | cmp - file
    

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

    tr -d '\000' <file | grep -q -m 1 ^ || echo All zeros
    

    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' <file | tr -c '\000' '\n' | grep -q -m 1 ^ || echo All zeros
    
    0 讨论(0)
  • 2021-01-04 08:02

    Completely changed my answer based on the reply here

    Try

    perl -0777ne'print /^\x00+$/ ? "yes" : "no"' file
    
    0 讨论(0)
  • 2021-01-04 08:06

    Straightforward:

    if [ -n $(tr -d '\0000' < file | head -c 1) ]; then
       echo a nonzero byte
    fi
    

    The tr -d removes all null bytes. If there are any left, the if [ -n sees a nonempty string.

    0 讨论(0)
  • 2021-01-04 08:12

    It won't win a prize for elegance, but:

    xxd -p file | grep -qEv '^(00)*$'
    

    xxd -p prints a file in the following way:

    23696e636c756465203c6572726e6f2e683e0a23696e636c756465203c73
    7464696f2e683e0a23696e636c756465203c7374646c69622e683e0a2369
    6e636c756465203c737472696e672e683e0a0a766f696420757361676528
    63686172202a70726f676e616d65290a7b0a09667072696e746628737464
    6572722c202255736167653a202573203c
    

    So we grep to see if there is a line that is not made completely out of 0's, which means there is a char different to '\0' in the file. If not, the file is made completely out of zero-chars.

    (The return code signals which one happened, I assumed you wanted it for a script. If not, tell me and I'll write something else)

    EDIT: added -E for grouping and -q to discard output.

    0 讨论(0)
提交回复
热议问题