Tips & How-To's

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!

68 Comments

  1. 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.

  2. 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.

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

      1. 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…??

  4. 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

  5. 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

  6. Pingback: Ashwin Saxena
  7. Pingback: Sathya
  8. 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

    1. 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..

  9. 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

        1. 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

  10. @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???

  11. 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?

  12. @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!

  13. 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)

  14. 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

  15. 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.

  16. 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

  17. 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

  18. Pingback: Sathya

  19. 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

  20. 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 ^_^

  21. 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

Leave a Reply

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