5 Linux commands for managing external drives – and how I use them


Ryan McVay/Getty Images

I currently have five external drives attached to my System76 Thelio, each of which serves a different purpose. I have one to house virtual machines, another for music files, another for backups, one for photos and videos, and one for miscellaneous things. Using these ensures the drive housing my operating system never fills up, which can cause slow-downs and lockups. 

Also: The first 5 Linux commands every new user should learn

Even if you have only one external drive, you’ll want to know the Linux commands available to help you manage them — especially when it comes to adding or removing those drives.

1. lsblk

The lsblk command comes in very handy when you need to know the name of a drive. If you want to permanently mount a drive (via the /etc/fstab file) or want to format a drive, you have to know the name of that drive (such as /dev/sdb). To locate that name, there’s no better command to use than lsblk

Issue that command and you’ll see a list of the drive names and the directories to which they are mounted. An lsblk listing will look something like this:

sda               8:0    0 931.5G  0 disk

└─sda1            8:1    0 931.5G  0 part  /media/jack/OLIVIA

As you can see, the device name is sda1 and it’s mounted to /media/jack/OLIVIA. If I were adding a new drive and needed to format it, I would know not to use the sda or sda1 names.

Also: 20 years later, real-time Linux makes it to the kernel – really

You can learn more about this command by reading the man page with man lsblk.

2. mkfs

Speaking of formatting, when you need to format a drive in Linux, you can either use a GUI tool like GNOME Disks or the mkfs command. Although formatting drives with mkfs isn’t exactly a task I’d hand over to someone with little Linux experience, it’s not as hard as you might think (beginners, check out these desktops). 

Also: Also: 5 Linux commands I use to keep my device running smoothly

For example, if you’ve used lsblk found found the drive to be formatted is sdb, the command to do so would be:

sudo mkfs -t ext4 /dev/sdb1

The -t option informs mkfs that we’re specifying a file system type and that type (in the above command) is ext4. One thing you might run into is if the disk hasn’t been partitioned. In that case, you’d use the fdisk command like so:

sudo fdisk /dev/sdb

3. blkid

When you want to permanently mount a drive with fstab, you might need to use a device ID and not a name. The blkid command lists block device attributes. One of the more important attributes is the UUID (Universally Unique Identifier) of a device. Using the blkid command also lists the label, block size, type, partition label, and the partition UUID. 

Also: The best external hard drives of 2024

When you want to permanently mount drives using fstab, it’s always best to use the UUID of a drive and this command will help you find it.

4. mount

This is one of the issues that never fails to confound new Linux users. When you attach an external drive, it has to be mounted to a directory so it can be viewed and/or used. Most Linux distributions are fully capable of doing this automatically. When that happens, the drives are typically mounted to the /media/USER (where USER is your username) directory. 

Also: 7 reasons why I choose Android over iOS (and sideloading is not one of them)

Sometimes, however, you might want to mount it to a different location — the mount command is what you’ll use. Mounting a drive with this command will look something like this:

sudo mount /dev/sdb1 ~/mount

The above command would mount the sdb1 partition on the sdb drive to the /home/USER/mount directory (where USER is your username). Once mounted, you would use the external drive as if it were located in ~/mount.

5. umount

When you need to remove an external drive, you use the umount command. That’s not a typo. You’d think the command would be unmount (since that’s what it does) but no, it’s umount. The command is very simple and looks a little something like this:

sudo umount /dev/sdb1

You could also use the mount point like so:

sudo umount ~/mount

Also: The Linux file system structure explained

Both commands would unmount the external drive so you can remove it.





Source link