3

I'd like to get (eventually in a systemd script) the device name of a USB drive that is connected (USB stick or an SSD drive). How can I do that reliably ? We can assume that there will be only one drive connected, but we don't know the UUID or anything else about it in this case. I mean something like this:

lsblk | grep disk | awk '{print $1}' | tail -n1

However, I'm not sure if the USB drive will be the last in that list always..

  • 2
    To filter usb you better use TRAN: lsblk -p -S -o NAME,TRAN | grep usb – guillermo chamorro Nov 4 at 15:59
  • Oh, cool! :) That seems to be useful. – user205301 Nov 4 at 16:01
4

If I understand you correctly, use the available columns of lsblk to print full information of the usb:

For example:

lsblk -p -o NAME,SERIAL,VENDOR,MODEL,TRAN | grep usb

Wich gives the output:

/dev/sdc    000FEA0AB9F5BA60641508A8 Kingston DT 101 G2        usb

Available columns (for --output):

        NAME  device name
       KNAME  internal kernel device name
     MAJ:MIN  major:minor device number
      FSTYPE  filesystem type
  MOUNTPOINT  where the device is mounted
       LABEL  filesystem LABEL
        UUID  filesystem UUID
    PARTTYPE  partition type UUID
   PARTLABEL  partition LABEL
    PARTUUID  partition UUID
   PARTFLAGS  partition flags
          RA  read-ahead of the device
          RO  read-only device
          RM  removable device
     HOTPLUG  removable or hotplug device (usb, pcmcia, ...)
       MODEL  device identifier
      SERIAL  disk serial number
        SIZE  size of the device
       STATE  state of the device
       OWNER  user name
       GROUP  group name
        MODE  device node permissions
   ALIGNMENT  alignment offset
      MIN-IO  minimum I/O size
      OPT-IO  optimal I/O size
     PHY-SEC  physical sector size
     LOG-SEC  logical sector size
        ROTA  rotational device
       SCHED  I/O scheduler name
     RQ-SIZE  request queue size
        TYPE  device type
    DISC-ALN  discard alignment offset
   DISC-GRAN  discard granularity
    DISC-MAX  discard max bytes
   DISC-ZERO  discard zeroes data
       WSAME  write same max bytes
         WWN  unique storage identifier
        RAND  adds randomness
      PKNAME  internal parent kernel device name
        HCTL  Host:Channel:Target:Lun for SCSI
        TRAN  device transport type
  SUBSYSTEMS  de-duplicated chain of subsystems
         REV  device revision
      VENDOR  device vendor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.