Format a USB from terminal

The most common thing that every one face while having a usb is that the usb doesn’t get detected when we try to insert it to the computer, or when we try to format it, it doesn’t format as it is not detected 😦

This problem can be easily removed on a *inux (Unix/Linux ubuntu is a Linux distribution).

First thing that we need to do to format a pen-drive is to find out where it is mounted by giving the df -H command in a terminal.

df -H

df is used to report file system disk space usage, and -H flag is used to specify the df command to print the size in human readable format 🙂 which is better to read than the bytes count. Here is the output of the above command in my computer.

Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb         32G   17G   16G  52% /media/c0mrad3/c0mrad3

here as it says the filesystem of the pendrive is /dev/sdb and it’s size is 32 Gb and used size is 17 Gb ie.. 52% and it is mounted on /media/c0mrad3/c0mrad3 usually it is mounted at /media/username/pendrive_name. Here both my system name and pendrive names are both c0mrad3 so it is mounted at /media/c0mrad3/c0mrad3.

so first we need to unmount the pendrive if it is mounted for this we need to know the filesystem at which it is mounted here it is /dev/sdb , usually pendrive starts with sdb and may contain someother extension in the ending too. usually sda means the harddisk and the partitions will be labled as sda1 sda2 and so on. Cd drive will at /dev/sr0.

sudo umount /dev/sdb

replace the sdb with what ever the file system of the pendrive be careful at this point as if you give the wrong thing you might loose all the data by formatting all the data in your hardisk. And the above command unmounts the pendrive or any other disk. Note that it is umount not un-mount as you might give a wrong command.
Next we format the pendrive using mkfs. mkfs builds a linux file system. Type the following command to format the pendrive to suit it for any type of os

sudo mkfs.msdos -n 'DHANVI' -I /dev/sdb

Here I have given the format as msdos as it works on almost all types of the systems. If you want to use the pendrive on newer system and make it faster then you can give the below command.

sudo mkfs.ext4 -n 'DHANVI' -I /dev/sdb

mkfs creates the file system mentioned after it. and -n is used to name the file system and better name the pen-drive as all capital letters as some file systems(MS-DOS) may not support name as lower letters.

And -I flag is used to explicitly say to format the entire device, usually we don’t format the entire memory device (hard disk) which will create problems, but since in the case of pen-drive we need to format the whole pen-drive, we need to say it explicitly.

So you are done with the formatting of pen-drive 🙂 The main use that I have by formatting the pen-drive from the terminal is that we can format almost any type of pen-drive that is problematic to format with any other software or the disk utilities in *inux


Feel free to comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s