Counting Non-Null Bytes of a File

The simplest form to count non-null bytes of a file is by using sed to delete the null bytes and then pipe it to wc. This will achieve our main goal to count the non-null bytes of a file.

sed 's/\x00//g' /path/to/file | wc -c

But, when trying to do the counting on large files, that command will take forever and it doesn’t even give any progress. So we can start using pv to be able to see the progress.

pv /path/to/file | sed 's/\x00//g' | wc -c

That’ll tell us how many bytes has been processed and the overall progress of the command. But, if you wanted to get more detail of the progress, we can put two pv in the command. This will give us the overall progress and the counted non-null bytes.

pv -N in -c /path/to/file | sed 's/\x00//g' | pv -N out -c | wc -c

The in part of pv will give us the overall progress and out is the counted non-null bytes.

Leave a Reply

Your email address will not be published. Required fields are marked *