Restore/Format USB Flash Drive

Who does not have a flash memory USB drive? (128Mb – 16Gb…) Everybody does, even my dog, if I had one, would have it. However, sometimes these little dongles can be tricky to use if they are not properly formatted. The usual format for the flash memory USB drive is FAT16, and it is understood by many operating systems with no problem. But in case the drive is not formatted properly/or you think it is not formatted properly, you can always reformat it, and here is how.

The whole process consists of 5 steps ans takes about 15 seconds um.. if do it slowly.. :)

In case you need to find the actual device your USB drive was associated with look here “Find USB flash device/drive in Linux

WARNING: All the data will be lost from the flash UBS drive since we are going to reformat it.

We are going to use fdisk utility, so “for starters” here are available commands that you can use with fdisk:

a - toggle a bootable flag
b - edit bsd disklabel
c – toggle the dos compatibility flag
d – delete a partition
l – list known partition types
m – print this menu
n – add a new partition
o – create a new empty DOS partition table
p – print the partition table
q – quit without saving changes
s – create a new empty Sun disklabel
t – change a partition’s system id
u – change display/entry units
v – verify the partition table
w – write table to disk and exit
x – extra functionality (experts only)

Now let’s get to cooking a new and fresh flash memory USB drive…

Step 1. Run fdisk via sudo on your USB flash drive device:

user@host:~$ sudo fdisk /dev/sdb

enter “p” command to list current partition table of the USB drive:

Command (m for help): p
 
Disk /dev/sdb: 1039 MB, 1039663104 bytes
32 heads, 62 sectors/track, 1023 cylinders
Units = cylinders of 1984 * 512 = 1015808 bytes
Disk identifier: 0xf1f494df
 
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1023     1014785   83  Linux

Here you will see whatever partition (if any) currently present. In my case (above) it is a “Linux” partition, in your case it may be something different. But it does not really matter, because we are going to delete it anyway:

Step 2. Enter “d” command to delete existing partition (if you have any). If you have several, delete several by using”d” command multiple times:

Command (m for help): d
Selected partition 1

Step 3. Now let’s get to business and enter “n” command to create a new partition:

Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1023, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1023, default 1023):
Using default value 1023

As you can see from above, I entered “1” for “Partition number”, and hit enter key twice to leave everything by default.

Now let us list current partitions with “p” command again:

Command (m for help): p
 
Disk /dev/sdb: 1039 MB, 1039663104 bytes
32 heads, 62 sectors/track, 1023 cylinders
Units = cylinders of 1984 * 512 = 1015808 bytes
Disk identifier: 0xf1f494df
 
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1023     1014785   83  Linux

We have a single “Linux” partition which is fine (for now).

Step 4. Time to actually apply our changes, so enter a “w” command that will write out all the changes to the USB drive.

Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.

fdisk will write all the changes to the USB drive and exit. But we are not done yet. Our goal is to make a (in this case) FAT16 partition, so this USB drive can be read by most operating systems.

Step 5. For that we will use mkfs.vfat utility:

user@host:~$ sudo mkfs.vfat -F 16 /dev/sdb1
mkfs.vfat 2.11 (12 Mar 2005)

Now we are good to go! It was easy, as I promised :)

Let’s look at the new USB drive now. Mounting it first:

user@host:~$ sudo mount /dev/sdb1 /mnt/flashka/
user@host:~$ cd /mnt/flashka/
user@host:/mnt/flashka$ ls -l
total 0

Checking what it has under the hood:

user@host:/mnt/flashka$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sdb1             991M     0  991M   0% /mnt/flashka
 
user@host:/mnt/flashka$

It has 991Mb which is what they call 1Gb flash memory USB drive! :)

33 comments

  1. Excellent way to restore any USB drive! I just went through all the steps with my old 128Mb flash that I could not use before, and it worked! Woohoo – thank you!

  2. Great tutorial..This is very nice website.

    Bookmarking it.

    Thanks !!

  3. thanks for tuto
    im having a problem here
    sudo mkfs.vfat -F 16 /dev/sdb1

    mkfs.vfat 2.11 (12 Mar 2005)
    /dev/sdb1: No such file or directory

    i try
    sudo mkfs.vfat -F 16 /dev/sdb
    but it show

    mkfs.vfat 2.11 (12 Mar 2005)
    mkfs.vfat: Will not try to make filesystem on full-disk device ‘/dev/sdb’ (use -I if wanted)

  4. You can also try using GParted if you don’t like using the command line.

  5. i have use GParted
    but its show “Error while setting new disklabel” hen i try to create partition “msdos”

  6. HideYourTracks.org,

    It seems like you did not complete steps before “sudo mkfs.vfat -F 16 /dev/sdb”, which is the last step in the tutorial above (btw – nice tutorial :) ).

    I did not have any problems with my 2Gb ScanDisk Thumb Drive.

    Thanks,

    J

  7. Is there a way to format it as ntfs?

  8. Thanks For You

  9. I am so very distressed as to usb flash drives. I made a mistake in thinking that I knew what I was doing and tried quick format but no joy. I have (2) 16GB flash drives, a Kingston, and a Standard. Both show up on My Computer, both will load files(music in my case) and you can play back, move, etc, but it does not retain all of them! If I load 6-8 GB on either, remove it from port, reinsert then some of the material is gone(empty) I am 71 years old, this is my 1st PC have had for 2 years. I share music with family and freinds all over the South in 7 states. I have the following HD devices:200GB Western Digital; 750GB Western Digital;1.5Terabyte Seagate. I have given away my two 4GB units to family with small children loaded with Baby Einstein, and Leapfrog programs for the kids and bought (2) 16GB’s to send to others with music, family photos(my Mother is 91 years old and I sort of keep everyone in touch) I also have a LaCie 20GB, and a new Verbatim 160GB usb both of which work just fine, but the two 16′s will not retain data, and when I sent to cousin, brother they files were blank or not downloadable. I will pay for someone to do what needs to be done as I have tried everything that I know of and am so frustrated. Please advise as I now apologize for being so “windy” and long in explanation.
    With Best Personal Regards
    Bill Michau
    Darlington, S.C.

  10. Bill,
    I am not sure what causing your problem. It also seems like you are using MS Windows, however the above article describes how to reformat flash on Linux (great article btw – thanks!). But maybe this will start you off: Causes of Data Loss.

  11. I didn’t get it at all. Is it for a Mac OS X computer?

  12. I forgot to add that my flash drive icon will not show up on the desktop. Could you help me?

  13. thanks for great article ;) I fixed my brother\’s in law flash key like that & now it\’s working perfectly :) but I have other problem… I have one more 2GB Kingston flash drive. it shows that it has only 8MB memory and in windows it says that it has RAW file system. using fdisk -l gives this information about it:

    \”Disk /dev/sdb: 8 MB, 8388608 bytes
    1 heads, 16 sectors/track, 1024 cylinders
    Units = cylinders of 16 * 512 = 8192 bytes
    Disk identifier: 0xffffffff

    Disk /dev/sdb doesn\’t contain a valid partition table.\”

    using all commands you gave above, it creates new partition but again only 8MB:

    \”Partition number (1-4): 1
    First cylinder (2-1024, default 2):
    Using default value 2
    Last cylinder, +cylinders or +size{K,M,G} (2-1024, default 1024):
    Using default value 1024

    Command (m for help): p

    Disk /dev/sdb: 8 MB, 8388608 bytes
    1 heads, 16 sectors/track, 1024 cylinders
    Units = cylinders of 16 * 512 = 8192 bytes
    Disk identifier: 0x19598c3d

    Device Boot Start End Blocks Id System
    /dev/sdb1 2 1024 8184 83 Linux\”

    and of course trying to make it FAT16 it says:

    \”WARNING: Not enough clusters for a 16 bit FAT! The filesystem will be
    misinterpreted as having a 12 bit FAT without mount option \”fat=16\”.\”

    is there any other options to fix this problem? how to force it to be 2GB again? :D

  14. i have deleated ally my songs from my flash drive by mistake and i want to restore it back help me please

  15. That was a useful tip. It helped recover data in one of my older USB flash drives.
    By
    Desktop Hard Drives

  16. I am getting error in step 4…. plz what to do…. help….

    Command (m for help): w
    The partition table has been altered!

    Calling ioctl() to re-read partition table.

    Error closing file

  17. Bean Machine

    Wow, this with maybe a combination of Test Disk worked for me. I can’t format it to EXT4 or anything else though.. but hey, I got my USB key back to an extent at least.

  18. how do i restore my 8gb

  19. I have a question, so I am trying to open my flashdrive but i cant, a message asks if i want to fromat it, ii dont want to format it becuase if I do I will lose the data I have saved, is there any way around it.

  20. i have a 8gb flash drive by PNY…….iv used it to transfer music from my mac to my laptop so i can put it on a micro sd card… and well today and yesterday ive had so many problems and its because all of the sudden all my memory is GONE….there is not a single file on my flash drive and it says there is only 1mb of usable memory…..please help me

  21. happy

  22. Really good explaination Thank you!

  23. Now will this change it back to normal as it was BEFORE I installed the linux installer onto it or what i dont really understand

  24. there is another way i just loaded puppy linux and used the partition maniger and there i saw 1 mb used and 3.5 gb unused i just deleted all and formated the flash drive to fat 16 and it workt :) sry but my english is a bit rusty :P

  25. How come you set up the partition as an ext2 then formatted it as a vfat? If you wanted a vfat wouldn’t you have set up the partition as ID b, W95 FAT32 using the “t” (toggle) command?

  26. I am getting error in step 4…. plz what to do…. help….

    Command (m for help): w
    The partition table has been altered!

    Calling ioctl() to re-read partition table.

    Error closing file

    plz help me…
    please send solution to praveenrao.uts@gmail.com

tell me something...
  1. (required)
  2. (valid email - optional)
  3. Captcha
  4. (required)