Formatting USB Flash Drive on Linux

Under UNIX-like operating systems, formatting a USB flash drive, in fact even a hard drive, requires knowing the file name for the partition that is to be formatted. These are files under the /dev directory, like for example, /dev/sdb1. So the interesting question to be asked is how does one come to know of the file representing the partition on the device? Immediately after connecting a USB flash drive to my laptop running Arch Linux and looking at the last few lines of ‘dmesg’ output gives:
sd 4:0:0:0: [sdb] 31703040 512-byte logical blocks: (16.2 GB/15.1 GiB)
Notice the ‘sdb’ within square brackets. This hints that the device file would start with /dev/sdb. Further, running:
$ ls -l /dev/sdb*
brw-rw---- 1 root disk 8, 16 Jan 31 23:55 /dev/sdb
brw-rw---- 1 root disk 8, 17 Jan 31 23:49 /dev/sdb1
And keeping it mind that it is a partition that is formatted, confirms the file that we are searching to be /dev/sdb1. Now, for formatting, if it is the EXT4 filesystem that is to be built then run:
mkfs.ext4 -L <label> /dev/sdb1
Use of the  -L <label> switch is optional. It gives a fancy name to the device that is often visible when automounting in a desktop environment like KDE or GNOME. After the above commands successful termination, one might want to print the partition table on the device for assurance:
# parted /dev/sdb print
Model: JetFlash Transcend 16GB (scsi)
Disk /dev/sdb: 16.2GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
1      16.4kB  16.2GB  16.2GB  primary  ext4         boot, lba
Notice that in the above case, the complete device (/dev/sdb) was given to parted and not just one partition. Manual mounting can be as simple as:
mount -t ext4 /dev/sdb1 <dir>
For more information, on Arch Linux, consider reading the man pages of dmesg(1),  mkfs(8), mount(8) and parted(8).

10 comments:

  1. in windows you just right click and click format

    ReplyDelete
  2. hate to bust your bubble, how-ever microsoft is way ahead of you guys in this department, litterally right click and click format then click okay for the warning box. So by pressing the mouse button 3 times it does what this entire article can do. So let's recap you could memorize all the commands and do it in about 200 button presses or you could do it in 3 button presses and learn no commands. Do you want to spend 2 minutes giving commands or 5 seconds? UP YOUR GAME CENTOS!

    ReplyDelete
  3. Hmm, too bad you didn't answer the question. I'm trying to make my flash drive ntfs.

    ReplyDelete
  4. Very Nice and Informative Post. Thank admin for sharing such an amazing post.

    --->>Unable to Format Pen Drive? (Format Guaranteed)

    ReplyDelete
  5. Thanks for sharing this- good Information! Keep up the great work, we look forward to reading more from you in the future!
    Regards,
    Linux Online Training
    Linux Training in Hyderabad

    ReplyDelete
  6. This is really awesome and beautiful.
    Thank for this sharing.
    Sony Pen Drives

    ReplyDelete
  7. Thanks for sharing such a great blog Keep posting..
    Linux Training in Delhi
    Linux Training Course in Delhi

    ReplyDelete
  8. I always like to extend my stay on this tech informative blog. This always helps me to enrich my tech knowledge that is really very hard to achieve from other sites. It was unknown how to format usb flash drive on linux operating system. But trailing to the bottom of the article I got the process very easy way. Thanks for sharing. Here I would like to add some information on custom usb flash drive. It brings creative styles to your promotional drive. This kind wholesale Bulk USB Drive is available for all types of USB Models, such as Wooden USB Stick, Plastic Memory Stick, Metal USB Drive, Leather USB Flash Drive, Cartoon Shaped USB Drive and OTG USB drives. custom usb cards It will be perfect for business promotion and company Ads. With customized Bulk USB logo printing, capacity selection, and casing design, all your flash drives will be full of original ideas. Get these custom drives for free printing, data preloaded and unique artwork from hugdiy.

    ReplyDelete