Format and Mount usb drive in GNU/Linux
Tools/commands:
- lsblk
- parted
- df
- fdisk
- mkfs
- wipefs
- check if which blcok devices used by usb (sd) [df -h, lsblk] eg:
a)
$ sudo fdisk -l
Disk /dev/sda: 7.2 GiB, 7736072192 bytes, 15109516 sectors
Disk model: DataTraveler 3.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000
b)
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 1 7.2G 0 disk
mmcblk0 179:0 0 15G 0 disk
├─mmcblk0p1 179:1 0 256M 0 part /boot
└─mmcblk0p2 179:2 0 14.7G 0 part /
- wipe the entire usb –YOU WILL LOSE ALL THE USB’s DATA–
$ wipefs --all --force /dev/sda
- create the partition by using parted
# parted /dev/sda
- create partition table (gpt or msdos) you can check by “p” – further info about different labels here– eg:
$(parted) mklabel msdos
- create the partition spanning the entire drive, and check by using “p”
(parted) mkpart primary fat32 0% 100%
(parted) p
Model: Kingston DataTraveler 3.0 (scsi)
Disk /dev/sda: 7736MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 7735MB 7734MB primary fat32 lba
- exit from parted [quit] and check the new partition on your system. eg:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 1 7.2G 0 disk
└─sda1 8:1 1 7.2G 0 part
mmcblk0 179:0 0 15G 0 disk
├─mmcblk0p1 179:1 0 256M 0 part /boot
└─mmcblk0p2 179:2 0 14.7G 0 part /
- build the filesystem by mkfs [mkfs.exfat -n usb /dev/sda1] eg:
# mkfs.exfat -n usb /dev/sda1
mkexfatfs 1.3.0
Creating... done.
Flushing... done.
File system created successfully.
- create a folder where to mount the devices [mkdir /media/usb-drive]
- finally mount the usb pointing the folder created [mount /dev/sda1 /media/usb-drive/]
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 1 7.2G 0 disk
└─sda1 8:1 1 7.2G 0 part /media/usb-drive
mmcblk0 179:0 0 15G 0 disk
├─mmcblk0p1 179:1 0 256M 0 part /boot
└─mmcblk0p2 179:2 0 14.7G 0 part /
Now you have access to the usb by the path: /media/usb-drive/