Linux mkdosfs Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux mkdosfs command, which is used to create a DOS filesystem on a partition or format a USB drive. The mkdosfs command is part of the dosfstools package, which provides utilities for creating and checking MS-DOS FAT filesystems. We will learn how to create a DOS filesystem on a partition, as well as how to format a USB drive with a DOS filesystem. This lab covers practical examples and the common options available for the mkdosfs command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") subgraph Lab Skills linux/dd -.-> lab-422805{{"`Linux mkdosfs Command with Practical Examples`"}} linux/sudo -.-> lab-422805{{"`Linux mkdosfs Command with Practical Examples`"}} linux/apt -.-> lab-422805{{"`Linux mkdosfs Command with Practical Examples`"}} end

Introduction to mkdosfs Command

In this step, we will learn about the mkdosfs command, which is used to create a DOS filesystem on a partition or format a USB drive. The mkdosfs command is part of the dosfstools package, which provides utilities for creating and checking MS-DOS FAT filesystems.

First, let's check if the dosfstools package is installed on our system:

sudo apt-get update
sudo apt-get install -y dosfstools

Example output:

Reading package lists... Done
Building dependency tree... Done
The following additional packages will be installed:
  libfuse2
The following NEW packages will be installed:
  dosfstools libfuse2
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.

The mkdosfs command is used to create a DOS filesystem on a partition or device. The basic syntax is:

sudo mkdosfs [options] <device>

Where <device> is the partition or device you want to format.

Some common options for the mkdosfs command include:

  • -F 12|16|32: Specifies the FAT type (12, 16, or 32 bits)
  • -n <volume-name>: Sets the volume name
  • -S <sector-size>: Sets the sector size (default is 512 bytes)
  • -c: Checks the device for bad blocks before formatting

Let's try creating a DOS filesystem on a partition:

sudo mkdosfs -F 32 /dev/sdb1

Example output:

mkdosfs 4.2 (2021-01-31)
/dev/sdb1 has 20971520 sectors of 512 bytes.

Creating a FAT32 filesystem in the volume with 20971520 available sectors.
Creating boot sector...
Creating FAT table...
Reserving space for root directory...
Writing directory entries...
Writing FAT tables...
Writing root directory...

In this example, we're creating a FAT32 filesystem on the /dev/sdb1 partition.

Creating a DOS Filesystem on a Partition

In this step, we will learn how to create a DOS filesystem on a partition using the mkdosfs command.

First, let's create a new partition on our system. We can use the fdisk command for this:

sudo fdisk /dev/sdb

Example output:

Welcome to fdisk (util-linux 2.38).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x1d7d1d7d.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519):

Created a new partition 1 of type 'Linux' and of size 10 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

In this example, we're creating a new primary partition on the /dev/sdb device.

Now, let's create a DOS filesystem on the new partition:

sudo mkdosfs -F 32 /dev/sdb1

Example output:

mkdosfs 4.2 (2021-01-31)
/dev/sdb1 has 20971520 sectors of 512 bytes.

Creating a FAT32 filesystem in the volume with 20971520 available sectors.
Creating boot sector...
Creating FAT table...
Reserving space for root directory...
Writing directory entries...
Writing FAT tables...
Writing root directory...

We've created a FAT32 filesystem on the /dev/sdb1 partition.

Formatting a USB Drive with DOS Filesystem

In this step, we will learn how to format a USB drive with a DOS filesystem using the mkdosfs command.

First, let's insert a USB drive into our system. We can use the lsblk command to identify the device:

lsblk

Example output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465.8G  0 disk
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 465.3G  0 part /
sdb      8:16   1   7.5G  0 disk

In this example, the USB drive is identified as /dev/sdb.

Now, let's format the USB drive with a DOS filesystem:

sudo mkdosfs -F 32 /dev/sdb

Example output:

mkdosfs 4.2 (2021-01-31)
/dev/sdb has 15523840 sectors of 512 bytes.

Creating a FAT32 filesystem in the volume with 15523840 available sectors.
Creating boot sector...
Creating FAT table...
Reserving space for root directory...
Writing directory entries...
Writing FAT tables...
Writing root directory...

We've created a FAT32 filesystem on the /dev/sdb USB drive.

Summary

In this lab, we learned about the mkdosfs command, which is used to create a DOS filesystem on a partition or format a USB drive. We first checked if the dosfstools package was installed on our system and then explored the basic syntax and common options of the mkdosfs command. We then demonstrated how to create a FAT32 filesystem on a partition using the mkdosfs command. Finally, we learned how to format a USB drive with a DOS filesystem.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like