Linux users feel empowered while performing tasks using the terminal application. So why leave the terminal when you can copy files to USB from the terminal app instead of a file explorer. This guide shows how to copy files and directories from a Linux system to a USB stick.
1. Know Where USB Mounted
Just before we begin, I am using Ubuntu 22.04 but the approach will mostly remain the same for any version and distribution.
Before we copy files, the USB stick needs to be mounted to your machine (laptop, PC, etc). Most Linux distributions mount the USB flash drive automatically to the system under the /media directory.
Insert the USB flash drive into your system. And let us find out the name of your USB drive using the fdisk command. Fdisk is used to manage hard drive and partitions in Linux.
sudo fdisk -l
It will display many disks but at the last, you will find the external disks. In the above image, you can see I have a 4 GB pen drive attached to my laptop. The /dev/sdb is the name of your USB. For you, the name may be different but mostly always named as sdc, sda, etc
If there is no output of your USB flash drive try using the following command :
lsusb
lusb is used to display all the connected and new devices on usb buses. As you can see in the above example I have a Transcend company's pen drive connected to the available USB port. If you see the output here and not in fdisk try reattaching or formatting your USB device using tools like Gparted.
Using lsblk command you can check the device name and its mount points. Here you can see sda mounted to /media folder.
2. Change to USB Mounted directory
Change the directory with the following command
cd /media
3. List Content Inside USB
Let's see what we have inside our Pendrive. Using the list directory command :
ls
4. Copy files to USB
Now launch a new Linux terminal and navigate to the folder you to copy files from. Linux cp command is used to transfer files from one folder to another.
I want to copy files from my home directory to the USB flash drive.
Copy single file:
sudo cp filename /media
To copy multiple files :
sudo cp -r filename1 filename2 filename /media
-r parameter is used to recursively copy content.
To copy a folder:
sudo cp -r foldername /media
To copy multiple folders:
sudo cp -r folder1 folder2 foldern /media
6. Verify
You can always verify that the files/folders have been copied using the ls command :
ls /media
You may also verify by its size as follows:
I have a 4 GB USB flash drive.
Conclusion
Unlike graphical mode where you can copy and paste files, using a terminal we need to use the copy command to transfer files to a mounted USB device. Once we identified the mounted USB directory, we can easily copy the files. As mentioned all modern Linux automatically mounts the USB devices that part is also easy.
Thanks for reading, please leave your suggestions and feedback in the below comment section.
Comments