Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

Format Disk using exFat on Command Line

September 14, 2023 — Gregg Szumowski

You may not always be working on a Linux system using a GUI, like a server or a system with very low resources. Sometimes you may need to format a disk to exFat using the Command Line. It’s not really that difficult.

The first thing you need to do is to know which device is to be formatted so before you connect the disk or USB make sure you run the lsblk command before and after doing so. This way you will be certain not to format the wrong disk by mistake and cause a data loss.

Run lsblk before inserting the disk:

# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda      8:0    0 232.9G  0 disk 
├─sda1   8:1    0   630M  0 part /boot/efi
├─sda2   8:2    0 216.3G  0 part /
└─sda3   8:3    0  15.9G  0 part [SWAP]
sdb      8:16   0   1.4T  0 disk 
├─sdb1   8:17   0   630M  0 part 
├─sdb2   8:18   0 244.1G  0 part /mnt/lfs
├─sdb3   8:19   0   1.1T  0 part /home
└─sdb4   8:20   0  15.6G  0 part 
sdc      8:32   1     0B  0 disk 
sr0     11:0    1  1024M  0 rom  

Then insert the disk and run lsblk again:

# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda      8:0    0 232.9G  0 disk 
├─sda1   8:1    0   630M  0 part /boot/efi
├─sda2   8:2    0 216.3G  0 part /
└─sda3   8:3    0  15.9G  0 part [SWAP]
sdb      8:16   0   1.4T  0 disk 
├─sdb1   8:17   0   630M  0 part 
├─sdb2   8:18   0 244.1G  0 part /mnt/lfs
├─sdb3   8:19   0   1.1T  0 part /home
└─sdb4   8:20   0  15.6G  0 part 
sdc      8:32   1     0B  0 disk 
sdd      8:48   1 979.8M  0 disk  <--+
├─sdd1   8:49   1    50M  0 part  <--|--New device
└─sdd2   8:50   1 928.8M  0 part  <--+
sr0     11:0    1  1024M  0 rom  

Compare the output and you’ll see that in this case, the device inserted was /dev/sdd.

Armed with that information, we can now begin our task. Start by invoking parted with the -a optimal option to specify the optimal alignment type:

root@slacker:~# parted -a optimal /dev/sdd
GNU Parted 3.4
Using /dev/sdd
Welcome to GNU Parted! Type 'help' to view a list of commands.

Next, set the partition table to gpt:

(parted) mktable gpt                                                      
Warning: The existing disk label on /dev/sdd will be destroyed and all data on
this disk will be lost. Do you want to continue?
Yes/No? Yes

Now, create a new partition table of exFAT type:

(parted) mkpart exFAT 0% -1                                               

Set the flag for the first partition to msftdata:

(parted) set 1 msftdata on
(parted) align-check opt 1
1 aligned
(parted) quit                                                             
Information: You may need to update /etc/fstab.

Now you should be done. Let’s print out the partition to make sure everything is OK:

# parted /dev/sdd print
Model: SanDisk U3 Cruzer Micro (scsi)
Disk /dev/sdd: 1027MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name   Flags
 1      1049kB  1027MB  1026MB  fat32        exFAT  msftdata

Now we can format it:

# mkfs.exfat -n exFAT /dev/sdd1
exfatprogs version : 1.1.3
Creating exFAT filesystem(/dev/sdd1, cluster size=32768)

Writing volume boot record: done
Writing backup volume boot record: done
Fat table creation: done
Allocation bitmap creation: done
Upcase table creation: done
Writing root directory entry: done
Synchronizing...

exFAT format complete!
# 

Tags: cli, exfat, parted, motd