Wipe a USB flash drive and recreate a filesystem

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











up vote
0
down vote

favorite












I'm using this to wipe a USB flash drive and recreate a FAT filesystem:



dd if=/dev/zero of=/dev/sdb bs=1M #I don't need more advanced wiping
fdisk /dev/sdb
(a few keystrokes to select partition type, etc.)
mkfs.fat /dev/sdb1


The fact that I have to manually do a few keystrokes is annoying. How could I do all of this in one step, without any intervention? Something like:



dd if=/dev/zero of=/dev/sdb bs=1M && ??? &&& mkfs.fat /dev/sdb1






share|improve this question
























    up vote
    0
    down vote

    favorite












    I'm using this to wipe a USB flash drive and recreate a FAT filesystem:



    dd if=/dev/zero of=/dev/sdb bs=1M #I don't need more advanced wiping
    fdisk /dev/sdb
    (a few keystrokes to select partition type, etc.)
    mkfs.fat /dev/sdb1


    The fact that I have to manually do a few keystrokes is annoying. How could I do all of this in one step, without any intervention? Something like:



    dd if=/dev/zero of=/dev/sdb bs=1M && ??? &&& mkfs.fat /dev/sdb1






    share|improve this question






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm using this to wipe a USB flash drive and recreate a FAT filesystem:



      dd if=/dev/zero of=/dev/sdb bs=1M #I don't need more advanced wiping
      fdisk /dev/sdb
      (a few keystrokes to select partition type, etc.)
      mkfs.fat /dev/sdb1


      The fact that I have to manually do a few keystrokes is annoying. How could I do all of this in one step, without any intervention? Something like:



      dd if=/dev/zero of=/dev/sdb bs=1M && ??? &&& mkfs.fat /dev/sdb1






      share|improve this question












      I'm using this to wipe a USB flash drive and recreate a FAT filesystem:



      dd if=/dev/zero of=/dev/sdb bs=1M #I don't need more advanced wiping
      fdisk /dev/sdb
      (a few keystrokes to select partition type, etc.)
      mkfs.fat /dev/sdb1


      The fact that I have to manually do a few keystrokes is annoying. How could I do all of this in one step, without any intervention? Something like:



      dd if=/dev/zero of=/dev/sdb bs=1M && ??? &&& mkfs.fat /dev/sdb1








      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 16 '17 at 8:27









      Basj

      6181731




      6181731




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Here-document syntax allows you to use fdisk non-interactively:



          fdisk /dev/sdb <<EOF
          n
          p



          t
          b
          p
          q
          EOF


          Because this is just an example, I used p and q so no changes are written. Use w after your verified sequence.



          Note a blank line corresponds to sole Enter. The point is you can pass your keystrokes this way.



          Alternatively you can write those lines (between two EOF-s) to a file, say fdisk.commands, and then:



          fdisk /dev/sdb < fdisk.commands


          Or without a file (from a comment, thank you Rastapopoulos):



          fdisk /dev/sdb <<< $'nnpnnnntnbnpnq'


          Another way:



          printf '%sn' "n" "p" "" "" "" "t" "b" "p" "q" | fdisk /dev/sdb



          There's also sfdisk. You may find its syntax more suitable for you.






          share|improve this answer






















          • OK, this was a mistake.
            – Kamil Maciorowski
            Oct 16 '17 at 9:16










          • Check my updated answer. You shouldn't expect you can do all of this in strictly one command. Unix philosophy says one tool should do one thing. You're doing three things.
            – Kamil Maciorowski
            Oct 16 '17 at 9:25






          • 1




            You can use <<<: fdisk /dev/sdb <<< $'nnpnnnntnbnpn', no?
            – Rastapopoulos
            Oct 16 '17 at 10:22






          • 1




            @Rastapopoulos You're right. Thanks for your input.
            – Kamil Maciorowski
            Oct 16 '17 at 10:31

















          up vote
          0
          down vote













          Based on @KamilMaciorowski's answer (full credit to him), here is what I finally use:



          sudo dd if=/dev/zero of=/dev/sdb bs=1M && sudo fdisk /dev/sdb <<< $'nnpnnnntnbnpnwn' && sudo mkfs.fat /dev/sdb1





          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f398351%2fwipe-a-usb-flash-drive-and-recreate-a-filesystem%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            Here-document syntax allows you to use fdisk non-interactively:



            fdisk /dev/sdb <<EOF
            n
            p



            t
            b
            p
            q
            EOF


            Because this is just an example, I used p and q so no changes are written. Use w after your verified sequence.



            Note a blank line corresponds to sole Enter. The point is you can pass your keystrokes this way.



            Alternatively you can write those lines (between two EOF-s) to a file, say fdisk.commands, and then:



            fdisk /dev/sdb < fdisk.commands


            Or without a file (from a comment, thank you Rastapopoulos):



            fdisk /dev/sdb <<< $'nnpnnnntnbnpnq'


            Another way:



            printf '%sn' "n" "p" "" "" "" "t" "b" "p" "q" | fdisk /dev/sdb



            There's also sfdisk. You may find its syntax more suitable for you.






            share|improve this answer






















            • OK, this was a mistake.
              – Kamil Maciorowski
              Oct 16 '17 at 9:16










            • Check my updated answer. You shouldn't expect you can do all of this in strictly one command. Unix philosophy says one tool should do one thing. You're doing three things.
              – Kamil Maciorowski
              Oct 16 '17 at 9:25






            • 1




              You can use <<<: fdisk /dev/sdb <<< $'nnpnnnntnbnpn', no?
              – Rastapopoulos
              Oct 16 '17 at 10:22






            • 1




              @Rastapopoulos You're right. Thanks for your input.
              – Kamil Maciorowski
              Oct 16 '17 at 10:31














            up vote
            2
            down vote



            accepted










            Here-document syntax allows you to use fdisk non-interactively:



            fdisk /dev/sdb <<EOF
            n
            p



            t
            b
            p
            q
            EOF


            Because this is just an example, I used p and q so no changes are written. Use w after your verified sequence.



            Note a blank line corresponds to sole Enter. The point is you can pass your keystrokes this way.



            Alternatively you can write those lines (between two EOF-s) to a file, say fdisk.commands, and then:



            fdisk /dev/sdb < fdisk.commands


            Or without a file (from a comment, thank you Rastapopoulos):



            fdisk /dev/sdb <<< $'nnpnnnntnbnpnq'


            Another way:



            printf '%sn' "n" "p" "" "" "" "t" "b" "p" "q" | fdisk /dev/sdb



            There's also sfdisk. You may find its syntax more suitable for you.






            share|improve this answer






















            • OK, this was a mistake.
              – Kamil Maciorowski
              Oct 16 '17 at 9:16










            • Check my updated answer. You shouldn't expect you can do all of this in strictly one command. Unix philosophy says one tool should do one thing. You're doing three things.
              – Kamil Maciorowski
              Oct 16 '17 at 9:25






            • 1




              You can use <<<: fdisk /dev/sdb <<< $'nnpnnnntnbnpn', no?
              – Rastapopoulos
              Oct 16 '17 at 10:22






            • 1




              @Rastapopoulos You're right. Thanks for your input.
              – Kamil Maciorowski
              Oct 16 '17 at 10:31












            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            Here-document syntax allows you to use fdisk non-interactively:



            fdisk /dev/sdb <<EOF
            n
            p



            t
            b
            p
            q
            EOF


            Because this is just an example, I used p and q so no changes are written. Use w after your verified sequence.



            Note a blank line corresponds to sole Enter. The point is you can pass your keystrokes this way.



            Alternatively you can write those lines (between two EOF-s) to a file, say fdisk.commands, and then:



            fdisk /dev/sdb < fdisk.commands


            Or without a file (from a comment, thank you Rastapopoulos):



            fdisk /dev/sdb <<< $'nnpnnnntnbnpnq'


            Another way:



            printf '%sn' "n" "p" "" "" "" "t" "b" "p" "q" | fdisk /dev/sdb



            There's also sfdisk. You may find its syntax more suitable for you.






            share|improve this answer














            Here-document syntax allows you to use fdisk non-interactively:



            fdisk /dev/sdb <<EOF
            n
            p



            t
            b
            p
            q
            EOF


            Because this is just an example, I used p and q so no changes are written. Use w after your verified sequence.



            Note a blank line corresponds to sole Enter. The point is you can pass your keystrokes this way.



            Alternatively you can write those lines (between two EOF-s) to a file, say fdisk.commands, and then:



            fdisk /dev/sdb < fdisk.commands


            Or without a file (from a comment, thank you Rastapopoulos):



            fdisk /dev/sdb <<< $'nnpnnnntnbnpnq'


            Another way:



            printf '%sn' "n" "p" "" "" "" "t" "b" "p" "q" | fdisk /dev/sdb



            There's also sfdisk. You may find its syntax more suitable for you.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 16 '17 at 10:31

























            answered Oct 16 '17 at 8:59









            Kamil Maciorowski

            1,0791523




            1,0791523











            • OK, this was a mistake.
              – Kamil Maciorowski
              Oct 16 '17 at 9:16










            • Check my updated answer. You shouldn't expect you can do all of this in strictly one command. Unix philosophy says one tool should do one thing. You're doing three things.
              – Kamil Maciorowski
              Oct 16 '17 at 9:25






            • 1




              You can use <<<: fdisk /dev/sdb <<< $'nnpnnnntnbnpn', no?
              – Rastapopoulos
              Oct 16 '17 at 10:22






            • 1




              @Rastapopoulos You're right. Thanks for your input.
              – Kamil Maciorowski
              Oct 16 '17 at 10:31
















            • OK, this was a mistake.
              – Kamil Maciorowski
              Oct 16 '17 at 9:16










            • Check my updated answer. You shouldn't expect you can do all of this in strictly one command. Unix philosophy says one tool should do one thing. You're doing three things.
              – Kamil Maciorowski
              Oct 16 '17 at 9:25






            • 1




              You can use <<<: fdisk /dev/sdb <<< $'nnpnnnntnbnpn', no?
              – Rastapopoulos
              Oct 16 '17 at 10:22






            • 1




              @Rastapopoulos You're right. Thanks for your input.
              – Kamil Maciorowski
              Oct 16 '17 at 10:31















            OK, this was a mistake.
            – Kamil Maciorowski
            Oct 16 '17 at 9:16




            OK, this was a mistake.
            – Kamil Maciorowski
            Oct 16 '17 at 9:16












            Check my updated answer. You shouldn't expect you can do all of this in strictly one command. Unix philosophy says one tool should do one thing. You're doing three things.
            – Kamil Maciorowski
            Oct 16 '17 at 9:25




            Check my updated answer. You shouldn't expect you can do all of this in strictly one command. Unix philosophy says one tool should do one thing. You're doing three things.
            – Kamil Maciorowski
            Oct 16 '17 at 9:25




            1




            1




            You can use <<<: fdisk /dev/sdb <<< $'nnpnnnntnbnpn', no?
            – Rastapopoulos
            Oct 16 '17 at 10:22




            You can use <<<: fdisk /dev/sdb <<< $'nnpnnnntnbnpn', no?
            – Rastapopoulos
            Oct 16 '17 at 10:22




            1




            1




            @Rastapopoulos You're right. Thanks for your input.
            – Kamil Maciorowski
            Oct 16 '17 at 10:31




            @Rastapopoulos You're right. Thanks for your input.
            – Kamil Maciorowski
            Oct 16 '17 at 10:31












            up vote
            0
            down vote













            Based on @KamilMaciorowski's answer (full credit to him), here is what I finally use:



            sudo dd if=/dev/zero of=/dev/sdb bs=1M && sudo fdisk /dev/sdb <<< $'nnpnnnntnbnpnwn' && sudo mkfs.fat /dev/sdb1





            share|improve this answer
























              up vote
              0
              down vote













              Based on @KamilMaciorowski's answer (full credit to him), here is what I finally use:



              sudo dd if=/dev/zero of=/dev/sdb bs=1M && sudo fdisk /dev/sdb <<< $'nnpnnnntnbnpnwn' && sudo mkfs.fat /dev/sdb1





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                Based on @KamilMaciorowski's answer (full credit to him), here is what I finally use:



                sudo dd if=/dev/zero of=/dev/sdb bs=1M && sudo fdisk /dev/sdb <<< $'nnpnnnntnbnpnwn' && sudo mkfs.fat /dev/sdb1





                share|improve this answer












                Based on @KamilMaciorowski's answer (full credit to him), here is what I finally use:



                sudo dd if=/dev/zero of=/dev/sdb bs=1M && sudo fdisk /dev/sdb <<< $'nnpnnnntnbnpnwn' && sudo mkfs.fat /dev/sdb1






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 16 '17 at 23:19









                Basj

                6181731




                6181731



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f398351%2fwipe-a-usb-flash-drive-and-recreate-a-filesystem%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    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