Check if a drive has been Zeroed with xxd and uniq?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2















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?










share|improve this question



















  • 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
















2















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?










share|improve this question



















  • 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














2












2








2


1






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?










share|improve this question
















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?







devices sort uniq hex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 18 '14 at 12:12







Tom Bennett

















asked Apr 18 '14 at 12:03









Tom BennettTom Bennett

112




112







  • 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













  • 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








1




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






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











3 Answers
3






active

oldest

votes


















1














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
*
0100000
$ od test
0000000 000000 000000 000000 000000 000000 000000 000000 000000
*
4000000


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



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


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.






share|improve this answer
































    1














    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 '' | head -c 1)" ]


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



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





    share|improve this answer






























      0














      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



      Thanks






      share|improve this answer






















        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f125380%2fcheck-if-a-drive-has-been-zeroed-with-xxd-and-uniq%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        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
        *
        0100000
        $ od test
        0000000 000000 000000 000000 000000 000000 000000 000000 000000
        *
        4000000


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



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


        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.






        share|improve this answer





























          1














          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
          *
          0100000
          $ od test
          0000000 000000 000000 000000 000000 000000 000000 000000 000000
          *
          4000000


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



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


          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.






          share|improve this answer



























            1












            1








            1







            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
            *
            0100000
            $ od test
            0000000 000000 000000 000000 000000 000000 000000 000000 000000
            *
            4000000


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



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


            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.






            share|improve this answer















            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
            *
            0100000
            $ od test
            0000000 000000 000000 000000 000000 000000 000000 000000 000000
            *
            4000000


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



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


            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 18 '14 at 12:33

























            answered Apr 18 '14 at 12:18









            GraemeGraeme

            25.1k46497




            25.1k46497























                1














                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 '' | head -c 1)" ]


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



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





                share|improve this answer



























                  1














                  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 '' | head -c 1)" ]


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



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





                  share|improve this answer

























                    1












                    1








                    1







                    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 '' | head -c 1)" ]


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



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





                    share|improve this answer













                    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 '' | head -c 1)" ]


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



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






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 19 '14 at 1:20









                    GillesGilles

                    534k12810781595




                    534k12810781595





















                        0














                        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



                        Thanks






                        share|improve this answer



























                          0














                          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



                          Thanks






                          share|improve this answer

























                            0












                            0








                            0







                            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



                            Thanks






                            share|improve this answer













                            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



                            Thanks







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 14 at 16:49









                            MVnD3XMVnD3X

                            11




                            11



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f125380%2fcheck-if-a-drive-has-been-zeroed-with-xxd-and-uniq%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown






                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay