DEV Community

Aryan Vaishnani
Aryan Vaishnani

Posted on

USB & External Media Handling

Linux can detect and manage:

  1. USB drives
  2. External hard disks
  3. SSDs
  4. Memory cards
  5. CD/DVD devices

These devices are usually mounted under:

/media

or

/mnt

  1. Detect Connected USB Devices

List USB Devices

lsusb

Shows connected USB hardware.

Example Output

Bus 001 Device 002: USB Flash Drive

  1. Check Storage Devices

List Disks

lsblk

Example Output

sda      100G
├─sda1    99G
sdb       16G
└─sdb1    16G

Here:

*sdb * is likely the USB drive.

1. Create Mount Point

Before mounting, create a directory:

sudo mkdir /mnt/usb

2. Mount USB Drive

Syntax

sudo mount /dev/sdb1 /mnt/usb

Meaning:

  • /dev/sdb1 = USB partition
  • /mnt/usb = mount point

Access USB Files

cd /mnt/usb

ls

3. Check Mounted Devices

df -h

Example Output

/dev/sdb1   16G   2G   14G

4. Unmount USB Drive

Before removing device:

sudo umount /mnt/usb

or

sudo umount /dev/sdb1

Important

Never remove USB storage without unmounting.

Otherwise:

  1. Data corruption may happen
  2. Files may be damaged

5. Automatic Mounting

Modern Linux distributions automatically mount USB drives under:

/media/username/

Example

/media/aryan/MyUSB

6. Filesystem Types

Common USB filesystem formats:

Filesystem Usage
vfat/FAT32 Windows compatibility
exFAT Large USB storage
NTFS Windows drives
ext4 Linux native filesystem

Check Filesystem Type

lsblk -f

7. Format USB Drive

EXT4 Format

sudo mkfs.ext4 /dev/sdb1

FAT32 Format

sudo mkfs.vfat /dev/sdb1

Warning

Formatting deletes all data.

8. Safely Remove Device

After unmounting:

udisksctl power-off -b /dev/sdb

Safely powers off USB device.

Real-World Usage

Backup Logs to External Disk

cp /var/log/syslog /mnt/usb

Transfer Kubernetes YAML Files

cp *.yaml /mnt/usb

Store Database Backups

mysqldump database > /mnt/usb/backup.sql

Useful Commands Summary

Command Purpose
lsusb Show USB devices
lsblk Show disks
mount Mount device
umount Unmount device
df -h Disk usage
mkfs.ext4 Format ext4
mkfs.vfat Format FAT32

Common Mount Locations

Directory Usage
/media Auto-mounted devices
/mnt Manual mounts

Best Practices

  1. Always unmount before removal
  2. Verify device name before formatting
  3. Use df -h to confirm mount
  4. Avoid mounting unknown USB devices on servers
  5. Use proper filesystem types for compatibility.

Top comments (0)