My thoughts on Tech

Formatting USB pen drive in Linux using Terminal

Insert your USB pen drive. Let it get detected and mounted. Open Terminal. Type The Following commands
1. dmesg |tail –> here the ‘|’ key is the pipe, ie, the key before the backspace key(the upper one, so press shift)
You’ll get something like

sathya@shaman:~$ dmesg |tail
[ 9921.681164] sda: Write Protect is off
[ 9921.681174] sda: Mode Sense: 23 00 00 00
[ 9921.681178] sda: assuming drive cache: write through
[ 9921.709138] SCSI device sda: 4030464 512-byte hdwr sectors (2064 MB)
[ 9921.720951] sda: Write Protect is off
[ 9921.720963] sda: Mode Sense: 23 00 00 00
[ 9921.720967] sda: assuming drive cache: write through
[ 9921.721225] sda:
[ 9921.727896] sd 0:0:0:0: Attached scsi removable disk sda
[ 9921.744187] sd 0:0:0:0: Attached scsi generic sg0 type 0
Note the terms in bold. In your system it will be different, maybe sdb or something. Whatever it may be, make sure to substitute it in the commands below, else your hard disk may get formatted.

2. Unmount your pen drive by using
sudo umount /dev/sda (In your case, please substitute sda with the appropriate device, listed above.

3. use the mkfs.vfat command to format to FAT32 filesystem, or mkfs.ext3 to format to ext3 filesystem
sudo mkfs.vfat -n ‘Label’ -I /dev/sda Replace Label with the name you want the pen drive to have.

4. That’s it! When done formatting, you’ll be returned to the prompt
sathya@shaman:~$ mkfs.vfat -n ‘sathya’ -I /dev/sda
mkfs.vfat 2.11 (12 Mar 2005)
sathya@shaman:~$

Remove and insert the pen drive to have mounted again!

If you enjoyed this post, make sure you subscribe to my RSS feed!

59 Comments

  1. <![CDATA[Subbu]]>'s Gravatar
    June 30, 2007    

    Hmm, Interesting, Never quite tried to do that in linux, I always used to do that in Windoze.

  2. <![CDATA[Samuel Baah]]>'s Gravatar
    July 24, 2007    

    Please how do i solve the problem below?
    how do i insert sbaah in sudoers file and where can i find the file
    iam a linux newbie

    [sbaah@Maint-Engineers ~]$ sudo umount /dev/sdb
    Password:
    Sorry, try again.
    Password:
    Sorry, try again.
    Password:
    sbaah is not in the sudoers file. This incident will be reported.
    [sbaah@Maint-Engineers ~]$ sudo umount /dev/sdb1
    sbaah is not in the sudoers file. This incident will be reported.

  3. <![CDATA[vinod]]>'s Gravatar
    August 1, 2007    

    Please umount the /dev/sda using root account.
    # sudo umount /dev/sda

  4. <![CDATA[Vaibhav Pandey-->Technofriends Team]]>'s Gravatar
    August 15, 2007    

    Pretty interesting.

  5. Matsiah's Gravatar Matsiah
    October 30, 2007    

    This was very helpful. I have a few difficulties — I interpreted the -I option as -l — but I found my errors and make it happen. Thanks.

    • November 19, 2013    

      I did as well. Please change the font for this command.

  6. January 1, 2008    

    Thanks a lot ;) gr8 blog post ;)

  7. Linuz's Gravatar Linuz
    March 24, 2008    

    Hey..can u explain in detail what the process behind this “mkfs.vfat” or “mkdosfs” is.what does it exactly do?i don quite get how the formatting is happening :)

  8. March 25, 2008    

    @Above, Explaining how they work is pretty technical stuff, I dont know how it works.

    You can have a look at the man pages:

    http://linux.die.net/man/8/mkfs.vfat

  9. Amit's Gravatar Amit
    September 30, 2008    

    mkfs.ext3: invalid inode size – /dev/sdc1
    what has happened here??/

  10. July 17, 2009    

    Thanks. It works. I was even able to get a 2 year old non-working USB (or so I thought) after following your steps.

  11. Thirupathi's Gravatar Thirupathi
    September 19, 2009    

    really its nice and worked well.thanks lot boss.

  12. October 2, 2009    

    thanx dear

  13. October 23, 2009    

    Nice piece of article. I got some errors. May be I should solve it and put on my blog. Thanks

    • October 24, 2009    

      Can you mention the errors you faced, Saurabh?

      • Vishnu's Gravatar Vishnu
        November 3, 2009    

        ubuntu@ubuntu:~$ umount /dev/sdc
        umount: /dev/sdc is not mounted (according to mtab)

        I get above result when i use the given commands… What is happenning…??

        • November 4, 2009    

          Vishnu it means the device is not mounted. Proceed with step 3 of the post.

  14. November 4, 2009    

    I got these errors at terminal-

    mkfs.vfat 3.0.1 (23 Nov 2008)
    mkfs.vfat: Device partition expected, not making filesystem on entire device ‘/dev/sdb’ (use -I to override)

    Now command is working with ‘I’ instead of ‘L’. It is ninth alphabet rather then 12th. I think theme font of blog is somewhat confusing between ‘l’ and ‘I’. I have read manual documentation of this command and experimented for different different formats on it . Thanks and God bless

  15. David Temple's Gravatar David Temple
    November 20, 2009    

    after the dmesg |tail command I get the following:

    FAT: Filesystem panic (dev sdb)
    fat_get_cluster: invalid cluster chain (i_pos 0)
    FAT: Filesystem panic (dev sdb)
    fat_get_cluster: invalid cluster chain (i_pos 0)
    FAT: Filesystem panic (dev sdb)
    fat_get_cluster: invalid cluster chain (i_pos 0)
    FAT: Filesystem panic (dev sdb)
    fat_get_cluster: invalid cluster chain (i_pos 0)
    FAT: Filesystem panic (dev sdb)
    fat_get_cluster: invalid cluster chain (i_pos 0)

    Can anyone help.

    It is a 16GB Kingston USB drive

    • November 20, 2009    

      David, seems that the filesystem is corrupted. Please proceed with formatting.

  16. srinu's Gravatar srinu
    December 29, 2009    

    plz tell any one how to format pen drive in linux

  17. January 25, 2010    

    Dude, did you really think that the pipe key was placed at the same position, in every single keyboard? :-o

    • January 25, 2010    

      LOL, in hindsight I realize that keyboard layout differ from country to country so the position would be lot different.

  18. letian's Gravatar letian
    June 8, 2010    

    omg, i formatted my windows partition C instead of usb!!!! “thanks” for help, could u please be MORE specific about sda and sbd, which do I choose, so next time I wont f..k my system

    • Natraj's Gravatar Natraj
      December 30, 2011    

      you can check with

      -> parted
      -> print all
      it will show you the entire disks connecte in your sys.. pendrive will appears as special device…

      dont do more experiments with parted command…improper use of parted results in lost of data..

  19. tower's Gravatar tower
    July 15, 2010    

    thanks

  20. ravi's Gravatar ravi
    August 29, 2010    

    root@ravi:/home/ravi# dmesg |tail
    [ 5639.791402] sd 4:0:0:0: [sdb] 65617920 512-byte hardware sectors: (33.5 GB/31.2 GiB)
    [ 5639.794872] sd 4:0:0:0: [sdb] Write Protect is on
    [ 5639.794879] sd 4:0:0:0: [sdb] Mode Sense: 03 00 80 00
    [ 5639.794887] sd 4:0:0:0: [sdb] Assuming drive cache: write through
    [ 5639.794898] sdb:
    [ 6589.481197] sd 4:0:0:0: [sdb] 65617920 512-byte hardware sectors: (33.5 GB/31.2 GiB)
    [ 6589.485998] sd 4:0:0:0: [sdb] Write Protect is on
    [ 6589.486005] sd 4:0:0:0: [sdb] Mode Sense: 03 00 80 00
    [ 6589.486016] sd 4:0:0:0: [sdb] Assuming drive cache: write through
    [ 6589.486033] sdb:
    root@ravi:/home/ravi# sudo umount /dev/sdb
    umount: /dev/sdb: not mounted
    root@ravi:/home/ravi# mkfs.vfat -n ‘ravi’ -I /dev/sdb
    mkfs.vfat 3.0.1 (23 Nov 2008)
    mkfs.vfat: unable to open /dev/sdb

    this is what happening…..
    could u help me out…..
    I was not able to open my pendrive

    • August 29, 2010    

      The log says write-protect is on. Try switching off write protect.

      • ravi's Gravatar ravi
        August 30, 2010    

        hi sathya…

        I am new to linux…

        could u let me know how to switch off write protect?

        Regards

        Ravi

        • August 31, 2010    

          Write protect is something done via the hardware. Is this a “Kingston” 32GB flash disk ? One you got it for real low price ? I have a feeling it’s a fake

  21. ravi's Gravatar ravi
    August 31, 2010    

    Yes u are exactly right, it is Kingston 32 GB flash drive…

    Is there any option to make it work?

    Regards

    Ravi

    • September 1, 2010    

      @Ravi: Out of curiosity, how much did you pay for it ?

  22. ravi's Gravatar ravi
    September 4, 2010    

    @Satya: Actually it was given to me by my friend. I think he purchased it for Rs. 600/- from other person, I am not sure abt it…..

    So now there is no solution to format this drive???

    • September 5, 2010    

      @Ravi: That’s a fake. Unfortunately, even if you somehow try to get it to work, it won’t retain any data.

  23. ravi's Gravatar ravi
    September 6, 2010    

    Thank you 4 info Sathya…

  24. Parth's Gravatar Parth
    November 5, 2010    

    hey i am following the steps but its not working in my case, probably you may help..

    [root@localhost ~]# dmesg|tail
    sd 12:0:0:0: [sdb] Write Protect is on
    sd 12:0:0:0: [sdb] Mode Sense: 03 00 80 00
    sd 12:0:0:0: [sdb] Assuming drive cache: write through
    sd 12:0:0:0: [sdb] 3860630417 512-byte hardware sectors: (1.97 TB/1.79 TiB)
    sd 12:0:0:0: [sdb] Write Protect is on
    sd 12:0:0:0: [sdb] Mode Sense: 03 00 80 00
    sd 12:0:0:0: [sdb] Assuming drive cache: write through
    sdb: unknown partition table
    sd 12:0:0:0: [sdb] Attached SCSI removable disk
    sd 12:0:0:0: Attached scsi generic sg2 type 0
    [root@localhost ~]# sudo mkfs.vfat -n ‘Label’ -I /dev/sdb
    mkfs.vfat 3.0.1 (23 Nov 2008)
    mkfs.vfat: unable to open /dev/sdb

    my pendrive was working before couple of days but now its not also its 4GB drive but its showing some TBs in the system.. what should i do?

  25. November 23, 2010    

    @Parth the line “sdb: unknown partition table” means that you don’t have any partition table on the pen. This means that you have to remake your partition table. You can use the command cfdisk for doing that. Check google for it.

    @sathya in you article you don’t explain users to replace /dev/sda in the command “sudo mkfs.vfat -n ‘Label’ -I /dev/sda” with the device name readed from dmesg.

    If in dmesg the usb pen is called /dev/sdb they have to use /dev/sdb in the format command like “sudo mkfs.vfat -n ‘Label’ -I /dev/sdb”..

    Without saying so they could end up formatting their own hard disk!

    • December 1, 2010    

      I just formatted my entire hard drive. ;) jk

      @Sathya: Thanks for the sharing this knowledge! I wish you changed it to sdb or sdx for safety.

  26. November 23, 2010    

    Ops… forget the second part of my comment above.. I overlooked that instruction.. sorry!

    • November 23, 2010    

      @Fabio: Steps 1 & 2 :-) Thanks for the re-reminder though :P

  27. swati's Gravatar swati
    February 4, 2011    

    when i follow u i got below output ,what i need to do please help me
    rgukt08@rgukt08-laptop:~$ dmesg |tail
    [ 21.319902] [drm] writeback test succeeded in 1 usecs
    [ 24.042181] atl1c 0000:06:00.0: irq 2300 for MSI/MSI-X
    [ 24.042598] ADDRCONF(NETDEV_UP): eth1: link is not ready
    [ 24.056992] ADDRCONF(NETDEV_UP): wlan1: link is not ready
    [ 74.182031] atl1c 0000:06:00.0: atl1c: eth1 NIC Link is Up
    [ 74.182231] ADDRCONF(NETDEV_CHANGE): eth1: link becomes ready
    [ 84.285073] eth1: no IPv6 routers present
    [ 529.126158] wlan1: Selected IBSS BSSID 82:be:8b:f7:80:bb based on configured SSID
    [ 539.921568] wlan1: no IPv6 routers present
    [ 574.261070] wlan1: disassociating by local choice (reason=3)

  28. swati's Gravatar swati
    February 4, 2011    

    hello sathya..!
    see this is the output
    after formating my pen drive i changed it name an other time it is not at all open even there is no devise icon also please help me as early as possible please

    rgukt08@rgukt08-laptop:~$ dmesg |tail
    [ 21.319902] [drm] writeback test succeeded in 1 usecs
    [ 24.042181] atl1c 0000:06:00.0: irq 2300 for MSI/MSI-X
    [ 24.042598] ADDRCONF(NETDEV_UP): eth1: link is not ready
    [ 24.056992] ADDRCONF(NETDEV_UP): wlan1: link is not ready
    [ 74.182031] atl1c 0000:06:00.0: atl1c: eth1 NIC Link is Up
    [ 74.182231] ADDRCONF(NETDEV_CHANGE): eth1: link becomes ready
    [ 84.285073] eth1: no IPv6 routers present
    [ 529.126158] wlan1: Selected IBSS BSSID 82:be:8b:f7:80:bb based on configured SSID
    [ 539.921568] wlan1: no IPv6 routers present
    [ 574.261070] wlan1: disassociating by local choice (reason=3)
    after formating pen drive i changed it name an other time it is not at all open even there is no devise icon also please help me as early as possible please

  29. February 4, 2011    

    Note that /dev/sda1 is the device for a partition. /dev/sda is the device for a drive. You want to mount the filesystem contained on a partition.

    Look at “ls -l /dev/sda*”. See if there is both /dev/sda and /dev/sda1
    Also read through the manpage for mount.

    If you system uses udev, you can get more information with:
    udevinfo -q env -n /dev/sda1

    The filesystem uuid number supplied can be used in an /etc/fstab entry in leu of a device name. The next time that you plug the drive in, a different device may be used to access it, so it is better to use either the label or the uuid if the file system instead.

    In the “man mount” manual, be sure to look at the “dmask”, “fmask”, “user”, “uid”, “gid” and “noauto” mount options. The “dmask”,”fmask”, “uid” and maybe “gid” would be used for a fat16, fat32 or ntfs filesystem. The noauto option would be used for any removable device. The “user” option will allow you to mount the filesystem as a regular user.

  30. Vimal Vishwakarma's Gravatar Vimal Vishwakarma
    February 10, 2011    

    dear sathya i tried the formating through linux but not succeed
    as u mentioned above all things was right
    but after giving the command
    sudo mkfs.vfat -n ‘Label’ -I /dev/sdb
    i came back to command prompt but any data was not deleted .
    hence the same problem persisting in my pendrive
    please help
    i did all the things what i can do
    i dont know much about linux
    give some suggestion

    • 123's Gravatar 123
      April 16, 2011    

      ditto for me. :(

  31. 123's Gravatar 123
    April 16, 2011    

    ditto for me, :(

  32. Sachith K N's Gravatar Sachith K N
    July 28, 2011    

    thankuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

  33. Nirtogen's Gravatar Nirtogen
    August 7, 2011    

    Thank you Sathya, your guide worked for me, I booted openSUSE from USB, so I wanted to try another LiveUSB OS, but I couldn’t clean my USB. Big thank you

  34. Natraj's Gravatar Natraj
    December 30, 2011    

    thank you all for sharing your experiences…

  35. venkat's Gravatar venkat
    January 25, 2012    

    nice instructions

  36. Niraj's Gravatar Niraj
    October 16, 2012    

    Thanks for sharing

  37. soni's Gravatar soni
    November 9, 2012    


    dmesg | tail
    [14926.035488] sd 14:0:0:0: [sdb] Write Protect is off
    [14926.035498] sd 14:0:0:0: [sdb] Mode Sense: 00 00 00 00
    [14926.035986] sd 14:0:0:0: [sdb] Asking for cache data failed
    [14926.035994] sd 14:0:0:0: [sdb] Assuming drive cache: write through
    [14926.040881] sd 14:0:0:0: [sdb] Asking for cache data failed
    [14926.040894] sd 14:0:0:0: [sdb] Assuming drive cache: write through
    [14926.157156] sdb:
    [14926.160109] sd 14:0:0:0: [sdb] Asking for cache data failed
    [14926.160114] sd 14:0:0:0: [sdb] Assuming drive cache: write through
    [14926.160120] sd 14:0:0:0: [sdb] Attached SCSI removable disk

    • soni's Gravatar soni
      November 9, 2012    

      can u tell me what should i do now to format my HP 4GB pendrive??

  38. syam's Gravatar syam
    November 10, 2012    

    mkfs works fine with fat and ext versions. But is there a way to change the filesystem to exFAT??

  39. xpto's Gravatar xpto
    March 22, 2013    

    Please, courier [new] in command lines… I had copied the commands to gedit before to be able read them…

  40. DensorJOHN's Gravatar DensorJOHN
    March 7, 2014    

    Sathya!! I can’t express my joy! After a long time of finding the solution to format my mbr write-protected USB, this was the only one that was working!! THANKS A LOT!!! *worshipping Sathya ^_^

    • DensorJOHN's Gravatar DensorJOHN
      March 7, 2014    

      Wait, it reverted to being read-only?!! Awww come on!!!

      • March 8, 2014    

        heh, sorry about that John :(

  41. chinnu's Gravatar chinnu
    May 6, 2014    

    hello Satya,
    I encountered a problem with my hard disc mounted in Ubuntu, I could not paste or cut or delete any files from my hard disc.Is there any way to use my harddisk without lossing any data in it?After running the command dmesg |tail , I got the following
    [ 4535.468562] scsi 8:0:0:0: Direct-Access StoreJet Transcend 0001 PQ: 0 ANSI: 2 CCS
    [ 4535.469560] sd 8:0:0:0: Attached scsi generic sg2 type 0
    [ 4535.473436] sd 8:0:0:0: [sdb] 976773167 512-byte logical blocks: (500 GB/465 GiB)
    [ 4535.474168] sd 8:0:0:0: [sdb] Write Protect is off
    [ 4535.474173] sd 8:0:0:0: [sdb] Mode Sense: 28 00 00 00
    [ 4535.474178] sd 8:0:0:0: [sdb] Assuming drive cache: write through
    [ 4535.476502] sd 8:0:0:0: [sdb] Assuming drive cache: write through
    [ 4535.476512] sdb: sdb1
    [ 4535.529101] sd 8:0:0:0: [sdb] Assuming drive cache: write through
    [ 4535.529109] sd 8:0:0:0: [sdb] Attached SCSI disk

    I hope that there will be a solution for this.

    Chinnu

3 Trackbacks

  1. Formatting USB pen drive in Linux using Terminal « Sathya Says on September 3, 2007 at 9:25 pm
  2. Formatting USB pen drive in Linux using Terminal | Herrmann on April 7, 2009 at 10:35 pm
  3. Tech??? Live: 39 ?? ????? ??????? ?? ???-??????? - Tech??? on October 28, 2009 at 2:03 pm
  4. Ashwin Saxena on November 23, 2009 at 3:33 pm
  5. Sathya on December 29, 2009 at 1:30 pm
  6. xinita » Blog Archive » ¿Cómo instalar Android en una Acer Aspire One? Es más fácil que burlarse de la iPad y toma menos neuronas que crearla on February 23, 2010 at 7:03 am
  7. Nightshift blog: Call Center Agent's Blog » I Love the Mac on July 4, 2010 at 6:32 am
  8. Sathya on February 5, 2012 at 2:14 pm
  9. mp3 powering but ''unknown device'' - Page 2 on January 9, 2013 at 12:56 pm

Leave a Reply