Friday, November 14, 2014

How to format Flash/Pen Drive in Linux


Note: I have used my device name and number in examples, you have to change it as your device name and number, otherwise unwanted device data will be wiped out.
Put your USB flash drive in one of your computer's free USB ports. After a few seconds, Linux will automatically recognize it and an icon for it will appear on your desktop. If pen drive doesn't mounted automatically, login as root and try to mount it, still it is not mounted there may be a problem with pen drive or it is defective.

Finding device:

Very very first thing is you should to know what device you are working with. For that you can use any one of the following methods.
Method 1: Using ls /dev/sd* command
First make sure your usb drive is not plugged in. Then run:
[venu@ras ~]$ ls /dev/sd* /dev/sda /dev/sda10 /dev/sda12 /dev/sda2 /dev/sda6 /dev/sda8 /dev/sda1 /dev/sda11 /dev/sda13 /dev/sda5 /dev/sda7 /dev/sda9
Once you have done that, put in your pen drive and run the above command again.
[venu@ras ~]$ ls /dev/sd* /dev/sda /dev/sda10 /dev/sda12 /dev/sda2 /dev/sda6 /dev/sda8 /dev/sdb /dev/sda1 /dev/sda11 /dev/sda13 /dev/sda5 /dev/sda7 /dev/sda9 /dev/sdb1
Whatever drive (ex sdb) was not there before is your pen drive. From the above list my pen drive is sdb. Don't be confuse with /dev/sdb and /dev/sdb1, sdb is a drive and sdb1 is first partition in that drive.
Method 2: Using df command
[venu@ras ~]$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda10 29753556 8008860 20208884 29% / /dev/sda12 19840892 1528664 17288088 9% /msfiles /dev/sda11 21825108 16772836 3925716 82% /venu tmpfs 1032232 0 1032232 0% /dev/shm /dev/sdb1 1947636 4 1947632 1% /media/VENU
As you can see above my pen drive (/dev/sdb1) is mounted at /media/VENU. If you have any doubt about which one is your pen drive, do same thing as in method1. Run df command before and after inserting pen drive.
Method 3: dmesg | tail
[venu@ras ~]$ dmesg | tail sdb: assuming drive cache: write through SCSI device sdb: 3903359 512-byte hdwr sectors (1999 MB) sdb: Write Protect is off sdb: Mode Sense: 0b 00 00 08 sdb: assuming drive cache: write through sdb: sdb1 sd 8:0:0:0: Attached scsi removable disk sdb sd 8:0:0:0: Attached scsi generic sg1 type 0 usb-storage: device scan complete SELinux: initialized (dev sdb1, type vfat), uses genfs_contexts
Method 4: mount -l
[venu@ras ~]$ mount -l /dev/sda10 on / type ext3 (rw) [/] proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) /dev/sda12 on /msfiles type ext3 (rw) [/msfiles1] /dev/sda11 on /venu type ext3 (rw) [/venu1] tmpfs on /dev/shm type tmpfs (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) /dev/sdb1 on /media/VENU type vfat (rw,nosuid,nodev,shortname=winnt,uid=500) [VENU]
Method 5: fdisk -l
fdisk command need root privileges. So login as root and run the command:
[root@ras ~]# fdisk -l Disk /dev/sda: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 5099 40957686 7 HPFS/NTFS /dev/sda2 5100 60800 447418282+ f W95 Ext'd (LBA) /dev/sda5 5100 10198 40957686 7 HPFS/NTFS /dev/sda10 50994 54817 30716248+ 83 Linux /dev/sda11 54818 57622 22531131 83 Linux /dev/sda12 57623 60172 20482843+ 83 Linux /dev/sda13 60173 60694 4192933+ 82 Linux swap / Solaris Disk /dev/sdb: 1998 MB, 1998519808 bytes 32 heads, 63 sectors/track, 1936 cylinders Units = cylinders of 2016 * 512 = 1032192 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 1936 1951456+ b W95 FAT32
fdisk -l displays all detected drives even drive is unmounted.

Formatting pen drive:

First decide which file system you want. FAT32 is probably the file system you want, but that's up to you. Ext3 may not be work on windows, It asks for fomatt- ing.
1.Once you have Identified your pen drive, unmount it.
[root@ras ~]# umount /dev/sdb1
2.Format pen drive now.
Depending on distribution different commands are available for formatting, but in general most used ones that I have seen are mkdosfs or mkfs.vfat for FAT32 and mke2fs or mkfs.ext3 for Linux filesystems.
For Ms-Dos or Windows file system:
Use mkdosfs -n "Label" -I /dev/sdx, Replace Label with the name you want the pen drive to have and /dev/sdx with your drive.
[root@ras ~]# mkdosfs -n "KVMREDDY" -I /dev/sdb1 mkdosfs 2.11 (12 Mar 2005)
or
[root@ras ~]# mkfs.vfat -n "KVMREDDY" -I /dev/sdb1 mkfs.vfat 2.11 (12 Mar 2005)
(Note: I – capital letter of i ) If you don't want any Label then no need to supply option n. switch I will force mkdosfs to work properly but it is not mandatory, you can remove that also.
For Linux File system:
Use mkfs.ext3 -L "Label" /dev/sdx for ext3 file system or mke2fs -L 'Label' /dev/sdx for ex2, Replace Label with the name you want the pen drive to have and /dev/sdx with your drive. Ext3 file system:
[root@ras ~]# mkfs.ext3 -L "KVMREDDY" /dev/sdb1 mke2fs 1.39 (29-May-2006) Filesystem label=KVMREDDY OS type: Linux ............................. [root@ras ~]#
ext2 file system:
[root@ras ~]# mke2fs -L 'VENU' /dev/sdb1 mke2fs 1.39 (29-May-2006) Filesystem label=VENU OS type: Linux ........................... [root@ras ~]#
After formatting you will be returned to the prompt then remove and insert the pen drive to have mounted again.

0 comments: