You are on page 1of 3

Formatting Flash Drive (Pen Drive) in GNU/Linux through CLI

Plug in your flash drive in USB port and wait for 10/15 seconds to let the
system detect it. Check that your system has recognized the drive.

root@mango:~# dmesg | tail


[15215.209739] scsi 5:0:0:0: Direct-Access Kingston DataTraveler 2.0 PMAP PQ: 0
ANSI: 0 CCS
[15215.210709] sd 5:0:0:0: Attached scsi generic sg2 type 0
[15215.616332] sd 5:0:0:0: [sdb] 15687680 512-byte logical blocks: (8.03 GB/7.48
GiB)
[15215.616939] sd 5:0:0:0: [sdb] Write Protect is off
[15215.616947] sd 5:0:0:0: [sdb] Mode Sense: 23 00 00 00
[15215.616953] sd 5:0:0:0: [sdb] Assuming drive cache: write through
[15215.622289] sd 5:0:0:0: [sdb] Assuming drive cache: write through
[15215.622302] sdb:
[15215.655795] sd 5:0:0:0: [sdb] Assuming drive cache: write through
[15215.655807] sd 5:0:0:0: [sdb] Attached SCSI removable disk

Here the flash drive is detected as `sdb’.

Wipe out everything on flash drive and fill every inch of it with zer0.

root@mango:~# cat /dev/zero > /dev/sdb

Don’t use partition here (eg. sdb1, sdb2 etc) but the device name (eg. sdb, sdc etc)

This command will take some time to complete depending on the size of the flash drive and exit with an
error saying can not write to the disk since disk is full. This error means we have filled the whole disk
with zeros.

Create partitions on your disk using fdisk

root@mango:~# fdisk /dev/sdc

This command will present you a following prompt

Command (m for help):

By pressing `m’ you will get to see various options provided by `fdisk’
Print the partition table

Command (m for help): p

Disk /dev/sdb: 8032 MB, 8032092160 bytes


248 heads, 62 sectors/track, 1020 cylinders
Units = cylinders of 15376 * 512 = 7872512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb0bcd68e

Device Boot Start End Blocks Id System

Command (m for help):

Since we have filled whole disk with zeros, we will get empty partition table.

Add 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-1020, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-1020, default 1020): 1020

Here we have just accepted all defaults to create single partition which takes entire free space
available on disk.

Print the partition table

Command (m for help): p

Disk /dev/sdb: 8032 MB, 8032092160 bytes


248 heads, 62 sectors/track, 1020 cylinders
Units = cylinders of 15376 * 512 = 7872512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb0bcd68e

Device Boot Start End Blocks Id System


/dev/sdb1 1 1020 7841729 83 Linux

We can now see one entry in partition table.


Change a partition's system id

Command (m for help): t


Selected partition 1
Hex code (type L to list codes): b
Changed system type of partition 1 to b (W95 FAT32)

Command (m for help): p

Disk /dev/sdb: 8032 MB, 8032092160 bytes


248 heads, 62 sectors/track, 1020 cylinders
Units = cylinders of 15376 * 512 = 7872512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb0bcd68e

Device Boot Start End Blocks Id System


/dev/sdb1 1 1020 7841729 b W95 FAT32

Command (m for help):

Write changes to disk and exit

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: If you have created or modified any DOS 6.x


partitions, please see the fdisk manual page for additional
information.
Syncing disks.

This will write changes to disk and create a new partition table on your flash drive.

Create a filesystem on our flash drive

root@mango:~# mkfs.vfat /dev/sdb1


mkfs.vfat 3.0.7 (24 Dec 2009)

Make sure you mention device partition here (sdb1) and not the entire device (sdb).

That’s it. Your flash drive is ready to get overloaded with files again...

- by greenmang0@irc.freenode.net

You might also like