Copying Files to a USB Thumb Drive Using the Linux Command Line on Raspberry Pi OS

 


Copying Files to a USB Thumb Drive Using the Linux Command Line on Raspberry Pi OS


Introduction

Working on a Raspberry Pi OS server with the Linux command line can feel a bit different than the usual desktop environment. While it’s simple to copy files and safely eject a USB drive from the desktop interface, doing so from the command line on a server requires a bit more attention. However, once you get used to the steps, you’ll find that it's straightforward and gives you more control. Here’s how you can do it.


Step 1: Insert the USB Drive and Identify It

Once you've inserted the USB thumb drive into the Raspberry Pi, you’ll need to find out where it’s mounted in the system. This can be done by checking the system logs or using a command like `lsblk`, which lists all block devices, including your USB drive.


$ lsblk


You should see your USB drive listed, often under a name like /dev/sda1 or /dev/sdb1, depending on your setup. Take note of this, as you’ll need it for the next steps.


Step 2: Create a Mount Point

To access the USB drive, you’ll need to mount it. First, create a directory that will act as the mount point. This is where the files on the USB drive will be accessible.


$ sudo mkdir /mnt/usb


Next, mount the USB drive to this directory:


$ sudo mount /dev/sda1 /mnt/usb


Be sure to replace /dev/sda1 with the actual device identifier from the previous step.


Step 3: Copy the File to the USB Drive

Now that the drive is mounted, you can copy the file you want from the Raspberry Pi to the USB drive. Let’s say you have a file called example.txt in your home directory:


$ cp ~/example.txt /mnt/usb


This command will copy example.txt from the home directory to the mounted USB drive. You can also copy entire directories if needed by using the -r option for cp to copy recursively.


Step 4: Safely Unmount and Eject the USB Drive

Once the file is copied, it’s crucial to safely unmount the USB drive to ensure data integrity. If you just pull out the drive without unmounting, you risk corrupting the data.


To unmount the drive, use the following command:


$ sudo umount /mnt/usb


If you receive a "device is busy" error, it means that something is still accessing the drive. You can check what processes might be using it with:


$ lsof /mnt/usb


After ensuring the drive is unmounted, you can safely remove the USB thumb drive from the Raspberry Pi.


Final Thoughts

Although the process may seem more involved than using a graphical interface, the command line method offers fine-grained control over file operations and device management. Once you’ve done it a few times, copying files to and from a USB drive on a command-line Raspberry Pi OS server will become second nature. Always remember to safely unmount your drives to avoid potential data loss.



Image: Saharsh Wadekar from Pixabay


Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison

The Reasoning Chain in DeepSeek R1: A Glimpse into AI’s Thought Process