linuxfileshell

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


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.


Solution

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