While using ‘dd’ is easy and convenient, its approach is such that it rearranges the partition layout of the USB storage device thus a formal disk format cannot be used to restore the storage device to its previous state, and until you restore the partition layout to its previous state, it cannot be used as a normal storage media either.
Now, I have written a ‘how to’ in the past describing a way in which Microsoft Windows 8/8.1 users can restore the changes. But since that is limited to Windows 7/8/8.1 users only, in this guide, I’ll show you how the same results too can be attained on GNU/Linux.
What we are going to do is quite simple. We will first delete the existing partition layout on the pendrive. Then we will create a single partition, occupying the whole space, and change its partition type to ‘FAT’ or ‘NTFS’ (otherwise it won’t get automatically mounted in GNU/Linux, thought this might not be a mandatory step if you’re going to use it only on Windows). At the final step, we will actually format the newly created partition into the desired file system type (‘FAT 32’ or ‘NTFS’).
Step 1: Make sure the USB pendrive is attached. Now open your terminal window and enter the below command:
sudo fdisk -l
‘fdisk’ is a powerful command-line based disk partition table manipulation utility that comes with GNU/Linux. When invoked with the ‘-l’ option, it scans and lists all the available disk drives. Carefully take a look at its output and find the section that displays information about the pendrive, specifically its ‘device path’, since that’s what we are looking for. It might look a bit confusing at first, but as long as you know the actual size of the pendrive, it should not be that difficult to find it.
For instance, on this occasion I have three storage devices attached to my laptop. One is my primary 320GB Toshiba drive, then there is the Western Digital 1TB USB drive and the 8GB Imation USB pendrive that requires the correction. As you can see in the above screenshot, the pendrive’s capacity is listed as 7.2 GiB (because it’s in ‘gibibytes’) and its device path is ‘/dev/sdc’.
Step 2: Now enter the below command, and make sure to replace the ‘/…/sdc‘ accordingly (if you enter the wrong device path, then you will lose all of its data in a short while!):
sudo fdisk /dev/sdc
Although not required for our purpose, you can hit the ‘p’ key and hit Enter for getting a list of all the partitions on the drive (in this instance, there should only be one).
To get rid of the existing partition simply press ‘d’.
Now we’re done deleting the partition, hit ‘p’ key again to make sure that there are no existing partitions. Then hit the ‘n’ key for creating a new partition. When asked what type partition you prefer ‘fdisk’ to create, press the ‘p’ key. This will create a singe primary partition.
In the next three steps, ‘fdisk’ will ask you for a partition number, a number for the First & the Last sectors. On all these occasions, just hit the Enter key so that ‘fdisk’ will use the default values. When done you will see a message conforming that a partition has been created with its type set to ‘Linux’.
Now if we let the type to be set as ‘Linux’, then GNU/Linux might struggle (Fedora 21 did at least) while trying to automatically mount the USB pendrive whenever you attach it to your computer. But that can be easily fixed by changing its type to ‘FAT 32’ or ‘NTFS’.
Changing the Label to ‘FAT 32’…
To change the label ‘FAT 32’ (assuming that you want it to be formatted into ‘FAT 32’), hit the ‘l’ key and then hit ‘b’.
This will change the type to ‘W95 FAT 32’ (don’t worry about the ‘W95’ part, this is merely a label for deceiving the operating system. The ‘FAT 32’ file system that we are going to format this partition into in the next step is an up-to-date one, and not the one initially included in Windows 95).
Changing the Label to ‘NTFS’…
To change the label to ‘NTFS’ (assuming that you want to format it into ‘NTFS’ in the next step), hit the ‘l’ key and then type the number 86 and then press Enter.
When you are done setting up the desired label, hit the ‘w’ key. This will write and apply all the changes we made above, to the disk, and when done, ‘fdisk’ will also automatically exit.
After completing that, now it’s time to make the OS aware of our newly created partition. To do that, enter the below command (here too make sure to replace the ‘/…/sdc‘ accordingly):
sudo partx -a /dev/sdc
If you get an error, ignore it (if however the OS refuses to let you format the newly created partition in the next Step, then reboot the computer).
Creating a ‘FAT 32’ File System...
Now all that is left is to actually create the ‘FAT 32’ file system on it. For that use the below command:
sudo mkfs -t vfat /dev/sdc1
Take a note here that now we are using the /sdc1‘ which refers to the partition, not the ‘/sdc’ which refers to the whole disk. And here too make sure to replace the ‘/…/sdc1‘ accurately. If you want to add a label when formatting then use the below command instead:
sudo mkfs -t vfat -n your_label_here /dev/sdc1
Creating a ‘NTFS’ File System…
To create a ‘NTFS’ file system, use the below command (make sure to replace ‘/…/sdc1‘):
sudo mkfs -t ntfs -f /dev/sdc1
To add a label while formatting, use the below command instead:
sudo mkfs -t nfts -f -L your_label_here /dev/sdc1
Now remove the USB pendrive and reattach it, and the OS should be able to mount it automatically. Well, that’s it!.