http://qs321.pair.com?node_id=906736

Steve_BZ has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I hope this is my last USB-drive-related question :) Does anyone know how to determine the label and drive capacity for a USB-drive under Linux? I looked under Device::USB and there doesn't seem to be anything and nothing else is jumping out at me from cpan.

Regards

Steve.

Replies are listed 'Best First'.
Re: How to get a USB-drive label
by NetWallah (Canon) on May 26, 2011 at 03:13 UTC
    Maybe you can parse the output of these commands, and piece the information together:
    ~$ lsusb .... Bus 001 Device 005: ID 0424:2602 Standard Microsystems Corp. USB 2.0 H +ub Bus 001 Device 010: ID 13fe:3100 Kingston Technology Company Inc. 2 GB + USB stick ... ~$mount ... /dev/sde1 on /media/0D5D-ABCE type vfat (rw,nosuid,nodev,uhelper=udisk +s,uid=1000,gid=1000,shortname=mixed,dmask=0077,utf8=1,showexec,flush) ~$ ls -l /dev/disk/by-path/ total 0 drwxr-xr-x 2 root root 340 2011-05-25 19:45 ./ drwxr-xr-x 6 root root 120 2011-05-25 12:42 ../ lrwxrwxrwx 1 root root 9 2011-05-25 19:42 pci-0000:00:02.1-usb-0:2.1 +.1:1.0-scsi-0:0:0:0 -> ../../sdc lrwxrwxrwx 1 root root 9 2011-05-25 19:42 pci-0000:00:02.1-usb-0:2.1 +.1:1.0-scsi-0:0:0:1 -> ../../sdd lrwxrwxrwx 1 root root 9 2011-05-25 19:45 pci-0000:00:02.1-usb-0:5:1 +.0-scsi-0:0:0:0 -> ../../sde lrwxrwxrwx 1 root root 10 2011-05-25 19:45 pci-0000:00:02.1-usb-0:5:1 +.0-scsi-0:0:0:0-part1 -> ../../sde1 lrwxrwxrwx 1 root root 9 2011-05-25 19:42 pci-0000:00:08.0-scsi-0:0: +0:0 -> ../../sda -- SO in this case, /dev/sde1 is the partition on my 2GB kingston key, + mounted at /media/0D5D-ABCE

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re: How to get a USB-drive label
by Khen1950fx (Canon) on May 26, 2011 at 03:46 UTC
    For me, this is the easiest way:
    #!/usr/bin/perl use strict; use warnings; system("fdisk -l /dev/sda");
Re: How to get a USB-drive label
by FunkyMonk (Chancellor) on Jul 14, 2011 at 23:25 UTC
    Just adding to the mix...

    What about parsing the output of df if you know where it's mounted