How to check if a file contains only zeros in a Li

2020-03-01 09:45发布

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 overkill.

标签: linux file shell
6条回答
我命由我不由天
2楼-- · 2020-03-01 10:05

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
查看更多
▲ chillily
3楼-- · 2020-03-01 10:10

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).

查看更多
姐就是有狂的资本
4楼-- · 2020-03-01 10:16

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.

查看更多
smile是对你的礼貌
5楼-- · 2020-03-01 10:16

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.

查看更多
我想做一个坏孩纸
6楼-- · 2020-03-01 10:17

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.

查看更多
我只想做你的唯一
7楼-- · 2020-03-01 10:29

Completely changed my answer based on the reply here

Try

perl -0777ne'print /^\x00+$/ ? "yes" : "no"' file
查看更多
登录 后发表回答