Formatting usb pen in Linux

Q. How do I format USB pen in Linux?

A. You can use standard mke2fs command to format usb pen in Linux. mke2fs is used to create an ext2/ext3 filesystem (usually in a disk partition or other USB devices). device is the special file corresponding to the device (e.g /dev/hdXX). blocks-count is the number of blocks on the device. If omitted, mke2fs automagically figures the file system size. If called as mkfs.ext3 a journal is created as if the –j option was specified.

Step # 1: Verify or find out your usb partition name

Type the following command to find out USB pen partition name:

# fdisk -l

Step # 2: Format parition

Once identified the partition name type the following command to format the usb pen in Linux (caution you must select correct usb partition name, otherwise you will loss all the data on hard disk):

# mkfs.ext3 /dev/sda1

To format as VFAT/FAT32 file system type the command:

# mkfs.vfat /dev/sda1

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 12 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
12 comments… add one
  • Mark Luxton Aug 15, 2008 @ 15:58

    I have been using Gparted to set up my USB pen.
    I find it near impossible to make mistakes this way.
    I had formatted it as FAT16 and it works going from Linux box to windows box and back. Eventually the pen would not allow me to add files even though there was over 3GB available. Could this be due to the deletion of files only removing the TOC and not the actual data?
    I just reformatted as FAT32 to see if that works better. Files are copying now. Is there a preferred format? FAT 16 or 32 or Linux Ext2 or 3. I realize it must be FAT to go to a Windoze box(or NTFS if all you got is Windoze).
    Note: At one point I could not get a large group of files to copy to the root of the pen drive but was able to do so into a folder on the drive???

  • Richard Steven Hack Oct 13, 2008 @ 21:48

    As for not being able to copy large number of files to a FAT16 root directy, I vaguely recall there is a limit to the number of files FAT16 supports in a root level directory. I think you can only a maximum of 512 files in a root directory because under the FAT16 system the root directory has a fixed size whereas subfolders do not.

  • Frank Feb 8, 2009 @ 23:35

    Hi guys,

    I have been pondering similar thoughts. It is my understanding that FAT16 in LBA mode is limited to a partition size of 2GB. Although, it has been reported that up to a 4GB partition is supported but is known to cause issues.

    Can anyone shine further light on the limitations of a FAT16 partition?


    Link #1 and Link 2

  • Jitu Jun 10, 2010 @ 17:34

    Hi, ALL

    Can anybody help in Linux, I want to my usb format when I went through this command #mkfs.vfat /dev/sda1 . This is not working and I got error like.mkfs.vfat: /dev/sda1 contains a mounted file system. what is this mean?


    • Anonymous J Coward Mar 21, 2011 @ 9:08

      This seems to have a high ranking on Google search, so let’s answer Jitu’s question.

      The error means that the USB disk is mounted. When mounted, it cannot be formatted. To format it, first un-mount it. E.g.

      #umount /dev/sda1

      (oh, and do double check sda is your USB drive – not your internal hard drive before formatting :), e.g. by running ‘df’ )

  • Mark Luxton Jun 11, 2010 @ 1:22

    I had forgotten about the post I made here till just now getting an email from the last poster. The original “problem” I was having was that I needed to empty the trash. Then I had the actual space to copy my files. I had deleted several GB’s of data from my 8GB USB pen, then I tried to copy several more GB’s to the pen. Although it seemed that I had the space, it wasn’t actually free till emptying the trash.
    FAT 32 seems to work best all around.
    Using Gparted to do the formatting is easy.

    Jitu. Perhaps you need to unmount /dev/sda1 before formatting from the command line? Also you may need to do this with root access. Mounted means that the drive is currently part of the Linux file system/operating system, and is “in use”.

    Make sure you have selected the correct drive. /dev/sda1 is most likely your hard drives first partition, which you can not format while you are running your operating system from it. That is a good thing!

  • Mark Jun 11, 2010 @ 11:48

    Jitu. You did not say what GNU/Linux you are using, but I will tell you exactly what I do to format MY USB pens.
    I have two hard drives and a DVD burner. Hard drive 1 is sda. Hard drive 2 is sdb.
    Gparted does not show me the DVD burner since it can not format it. With my USB pen plugged in, Gparted lists it as sdc or sdc1(First and only partition).
    I use these instructions for Ubuntu 8.04 and 10.04.

    1/ Be connected to the internet.
    2/ From a command line terminal do: sudo apt-get install gparted
    Give root password.
    3/ Plug in your USB pen.
    4/ Run Gparted, giving your root password when asked.
    5/ Click the drop down box in the upper right hand corner of Gparted’s window. Identify which “drive” is your USB pen. Mine is hdc1.
    6/ From command line terminal or console window do : sudo umount /dev/sdc1
    7/ You may have noticed a set of keys symbol after /dev/sdc1, in the line describing your USB pen in Gparted, which means it was mounted.
    8/ In Gparted’s menu click Refresh Devices. The keys symbol is now gone.
    9/ Click on the line describing your USB pen, in Gparted, to highlight it.
    10/ Right click on the line, choose “Format to” and “fat32”.
    11/ Click the green arrow to apply your choices.
    12/ Close Gparted.
    13/ Unplug your USB pen. Plug it back in. It should be mounted automatically and be ready to use.
    14/ Remember to empty the “Trash” now and then after deleting lots of files from the USB pen so as to actually free up the space.
    Works for me!

  • Anons Jul 26, 2012 @ 1:30

    You did not say that the flash drive must be unmounted!!

  • Philippe Petrinko Nov 8, 2012 @ 15:36

    Hi Vivek,

    Reading this old post, it would be great to modify some points:

    1) Replace /dev/sda1 with another example as a file device. This device is most likely PC main hard drive partition. This is really dangerous for newbies. Try “/dev/sdc1” for instance.

    2) As “anons” pointed out, unmounting USB drive should be shown, as it has probably been automagically mounted when connected, as configured for most of desktop Linux distributions. At least this should be checked.

    — Philippe

  • Mac Jan 3, 2013 @ 14:15

    How do you set a label on the USB thumb drive for example after you format it in NTFS?

    I am searching for it via the command line but not finding anything?

    Does anyone know?

    Would I do this:

    fdisk -l

    Disk /dev/sdc: 4026 MB, 4026531840 bytes
    128 heads, 30 sectors/track, 2048 cylinders
    Units = cylinders of 3840 * 512 = 1966080 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0xc3072e18

    Device Boot Start End Blocks Id System
    /dev/sdc1 1 2048 3932145 87 NTFS volume set

    I changed it via fdisk /dev/sdc1 to type ’87’

    then I formatted it using:

    wex01 $ mkfs.ntfs /dev/sdc1
    Cluster size has been automatically set to 4096 bytes
    Initizializing device with all zero’s

    is this correct – then how do I label the device ?

  • cherry Jan 12, 2014 @ 5:13

    be careful,
    if /dev/sda1 is the default partition of Windows if you happen to dual boot with windows in your hard disk. I am NOT into computers and these guys posting here are making fun of you. Be careful with fdisk command as root, especially the examples here, you can format your hard disk, with linux, with windows or with any other OS or important data. Do a backup before running fdisk

  • Mohammad Etemaddar Apr 26, 2014 @ 17:00

    Cyberciti, You are great!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum