Formatting and Mounting a USB Drive from a Terminal Window

Formatting and Mounting a USB Drive from a Terminal Window

USB Pen DriveYou can partition and format a USB memory stick or hard drive from a terminal window on the Raspberry Pi.  This removes ALL information from your memory stick/hard drive, so make sure you want to do that before you continue.

First, you need to identify which device is your USB stick - the easiest way is to remove it (if it is plugged in) and then reconnect it. This will generate some messages about the device in the system messages file. View the last few lines to identify your device.  Your device will be named something like sda, sdd, etc.  In this tutorial we will use the example /dev/sdb

cat /var/log/messages | tail -n 50

You may find that it has already been mounted as a usable drive.  If it has, you will see it in the /media directory.  Take a look:

cd /media

If there is more than one, then you may have more than one memory stick on your Raspberry Pi.  Identify which one is the one you want to format and take a note of its name, e.g. '1GBStick'. Remember that in Linux, file names are case sensitive.

Before we can reformat the stick, we must first 'unmount' it so that the operating system no longer sees it as a usable drive:

umount /media/1GBStick

The next step is to create a partition on the stick.

sudo fdisk /dev/sdb

You will be prompted with:

codeCommand (m for help):code

Type 'p' to see all the partitions that already exist:

Disk /dev/sdb: 1040 MB, 1040449536 bytes
33 heads, 61 sectors/track, 1009 cylinders, total 2032128 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd2c04bc9

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10000 3976+ 83 Linux
/dev/sdb2 10001 2032127 1011063+ 83 Linux

This time we are going to delete them all, so type 'd' to delete any partitions that already exist, typing in the partition number at the prompt that you want to delete - this is the number after the /dev/sdb.  Repeat this to delete all partitions.

Now we want to create a new partition.  Type 'n' to create a new partition.  You will be prompted to make some decisions, but since we are going to use the whole stick as a single partition, take the defaults by pressing return four times.

To complete the job, type 'w' to write the new partition table and exit fdisk.

Now, create a filesystem using the mkfs command.  If you use the vfat file system you will be able to use the memory stick on a PC or a Mac too.  There is one partition, so you must put the partition number onto the end of the device name:

sudo mkfs -t vfat -I /dev/sdb1

Now you need to create a folder as a mount point for the device.  Go to the directory where you want to create the mount point, for example, your home directory:

cd ~

And create the mount point with any name you want use.  Here I'm going to use 'usbstick'

mkdir usbstick

Finally, you can now connect the device to its mount point:

sudo mount /dev/sdb1 usbstick -o umask=000

Now you have a memory stick or hard drive that you can write to on your Raspberry Pi.

As with all USB memory sticks and hard drives, you must tell the operating system to finish writing to it and prepare it for removal.  On the Raspberry Pi all you have to do is unmount it:

cd ~
sudo umount usbstick

Now you have a newly formatted USB stick or hard drive that you can use in both your Raspberry Pi and a PC/Mac.

Featured Products

Raspberry Pi 3 Model B+ - The Pi HutRaspberry Pi 3 Model B+ - The Pi Hut
Raspberry PiRaspberry Pi 3 Model B+
Sale price £33.60 incl. VAT excl. VAT
Raspberry Pi Zero W - The Pi HutRaspberry Pi Zero W - The Pi Hut
Raspberry PiRaspberry Pi Zero W
Sale price £13.50 incl. VAT excl. VAT

Leave a comment

All comments are moderated before being published.

This site is protected by hCaptcha and the hCaptcha Privacy Policy and Terms of Service apply.