Richard Smedley presents your cut-out-and-keep guide to using the command line on the Raspberry Pi. In this part, we tackle the management of removable storage
Although Raspbian will, when booted as far as the GUI, automatically mount any disk-type device (USB flash key, camera, etc.) plugged into the USB port and offer to open it for you, you may wish to get more direct control of the process. Or, as is more often the case, you may want to mount a disk when the Raspberry Pi is running a project that doesn’t involve anyone getting as far as typing startx at the command line, as such graphical fripperies aren’t necessary for most connected devices.
For more command line tips and projects such as this, you should check out our brand new Ebook, Conquer the Command Line. It’s available on all the usual digital services so you could own it instantly.
Plugging a drive or flash memory device into your Pi (connecting it to your computer) is not the same as making it available for the Pi to interact with (mounting it) so that Raspbian knows what’s on it and can read, write, and alter files there. It’s an odd concept to accept: the computer knows there’s a disk plugged in, but its contents remain invisible until the Pi is told to mount it. It’s a bit like seeing a book on your shelf, but not being allowed to open or read it.
Disks and disk-like devices are mounted by Raspbian on a virtual file system, and you’ll rarely need to worry about what goes on beneath that layer of abstraction, but to see some of it, type mount. The information displayed is of the form device on mount point, file-system type, options. You’ll see a lot of device “none” for various bits of the virtual system that you don’t need to worry about; the devices that concern us start with /dev/ and have names like /dev/mmcblk0p1 for partitions of the Pi’s SD card, and /dev/sda1 for plugged-in USB drives.
Plug in a USB drive (remember that the Pi is not happy to power drives itself: either use a powered drive, or plug a USB flash drive into a powered USB hub). If you haven’t yet typed startx , then the disk will not get automatically mounted; if you have, then you need to unmount it. mount will show an entry beginning something like “ /dev/sda1 on /media/FLASH DRIVE… ” and you can unmount with sudo umount /dev/sda1 (yes, that is umount without an “n”). An error will result if the device is in use, so change directory and/or close apps using files from the device. Now we can mount it just the way we want.
The /dev/sda1 refers to the first (or only) partition on /dev/sda . The next device plugged in will be /dev/sdb1 . You can see what’s being assigned by running tail -f /var/log/messages , then plugging in the USB device. On other Linux systems, if /var/log/messages draws a blank, try /var/log/syslog . Stop the tail with CTRL+C . Another way of seeing connected devices that aren’t necessarily mounted is with fdisk, a low-level tool used to divide disks up into partitions, before creating file systems on those disks (see the “Format” boxout). Called with the list option sudo fdisk -l , it performs no partitioning, but simply lists partitions on those disks connected to your Pi. It also gives file-system information, which you need in order to mount the disk. Lastly, you need a mount point (somewhere to place the device on the file-system hierarchy) with appropriate permissions. Create one with:
$ sudo mkdir /media/usb
$ sudo chmod 775 /media/usb
You can then mount the disk with sudo mount -t vfat /dev/sda1 /media/usb , where vfat (or ntfs or ext2) is the file-system type.
Raspbian knows which disks to mount at boot time by reading the file-system table ( /etc/fstab ), and we could put our /dev/sda1 in there, but if we start up with two drives plugged in, the wrong one may be selected. Fortunately, disks (or rather, disk partitions) have unique labels known as UUIDs randomly allocated when the partition is created. Find them with sudo blkid , which also helpfully tells you the label, if any, that often contains the make and model of external drives, or look in /dev/disk/by-uuid .
For an NTFS-formatted drive, we called sudo nano /etc/fstab and added the following to the end of the file:
/dev/disk/by-uuid/E4EE32B4EE327EBC /media/usb1t ntfs defaults 0 0
This gives the device name (yours will be different, of course), mount point, file-system type, options, and two numeric fields: the first of these should be zero (it relates to the unused dump backup program), while the second is the order of check and repair at boot: 1 for the root file system, 2 for other permanently mounted disks for data, and 0 (no check) for all others. man mount will tell you about possible options.
We touched briefly on nano in part 2 of this series. Looking in a little more depth, the first thing to be aware of is the dozen shortcuts listed across the bottom two lines of the terminal: each is the CTRL key (represented by the caret ^ ) held at the same time as a single letter key. For example, ^R for ReadFile (i.e. open), ^O for WriteOut (in other words, save), and ^X for Exit. Remember those last two for now, and you’ll be able to manage nano. However, if you learn more of them, you will really race through your editing tasks.
Nano lacks the power features of Emacs and Vim, its two main command-line code editor rivals, but has useful features such as a powerful Justify ( ^J ), which will reassemble a paragraph of line-break strewn text into an unbroken paragraph, or apply line breaks at a fixed character length.This is a legacy of its development for email composition. ^K cuts the line of text the cursor is on, but it isn’t just a delete function: each cut is added to a clipboard. ^U will paste the entire clipboard at the cursor position: it’s great for gathering together useful snippets from a longer text.
Hit ^O to save fstab, and the shortcut listing changes, with many now beginning M instead of ^ – this is short for Meta, which means the ALT key on your keyboard (once upon a time, some computers had several modifier keys, such as Super and Hyper). One “hidden” shortcut after ^O is that at this point, ^T now opens a file manager to search for the file/directory where you want to save.
After saving, exit nano; now sudo mount -a will mount the external drive at the desired mount point (above), regardless of what else is plugged in. If you have other new entries in /etc/fstab , then sudo mount /media/usb1t (or whatever entry you put in fstab) will mount just that chosen device if you don’t want to mount any of the others.