I'm looking for a way to check if the entire binary contents of a USB device are purely 0s.

I'm trying this command: sudo xxd /dev/sdb | uniq but it has no output and just runs forever.

I've considered the Sort command, but my understanding is that it requires a file to read rather than just the standard terminal output.

update: I realised I could check with sudo xxd -a /dev/sdb, but my question still stands: is there a way to uniquely sort output of the terminal without saving it to a file first?

  • 1
    cmp /dev/sdb /dev/zero should be cmp: EOF on /dev/sdb. As for sort, it's just something | sort, nothing special about it, except you'll run out of memory if you try dumping gigabytes of zeroes into it... – frostschutz Apr 18 '14 at 19:13

Simply using od or hexdump should be fine since these programs avoid outputting repeated lines (or use xxd -a as above). Eg:

$ truncate -s 1M test
$ hexdump test
0000000 0000 0000 0000 0000 0000 0000 0000 0000
$ od test
0000000 000000 000000 000000 000000 000000 000000 000000 000000

If the drive is zeroed then the output won't be much different than this except that the final address will be much larger. If you see any other data, you could just hit Crtl-C.

To avoid continuing after non-zero data has been found and filling up the terminal, you could do something like this (in bash):

stdbuf -oL od /dev/sdb |
  while read line; do
    echo "$line"
    (( ++count > 3 )) && break

This will print at most 4 lines of output, from which you can tell if the drive is zeroed or not.

Top have done this dirtily with sort, you could have done sudo xxd /dev/sdb | sort -u. Adding the -u option to sort is equivalent to doing sort | uniq.


xxd doesn't strike me as the right hammer for this screw. You can run tr to remove null bytes and see if there's anything left:

[ -n "$(</dev/sdb tr -dc '\0' | head -c 1)" ]

You can also use od, which collapses null-filled lines:

[ "$(od -tx1 -An -w1 | head -n 2 | tr -d ' \n')" = "00*" ]

i think this command will do the trick for you

xxd /dev/sdb | grep -v '0000 0000 0000 0000 0000 0000 0000 0000'

if the drive has been zeroed this command will return no output. Incase it has any bytes left on it, command will be happy to prompt it on the screen


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.