2

My objective is to determine the name of a mounted USB flash drive programmatically in linux using C.

Has anyone got any suggestions?

Alex P.
  • 30,437
  • 17
  • 118
  • 169
Kallel Omar
  • 1,208
  • 2
  • 17
  • 51
  • What do you mean with _the name of detected USB flash drive_? – LPs Dec 26 '16 at 09:06
  • I mean the display name, the name of the device that I specify when I format it – Kallel Omar Dec 26 '16 at 09:15
  • You do not format a disk but rather a partition (aka volume). The name you assign to it after formatting is called a *volume label*. To query the volume label you do not use any `USB` API but rather file system API. – Alex P. Dec 26 '16 at 23:49
  • you can use the command: `lsusb -v -t` to determine where the device is mounted, Then pass that address as a parameter to your program – user3629249 Dec 27 '16 at 05:36

1 Answers1

0

think the USB stick is registered as /dev/sdb (you can determine which is the USB stick in /dev using sudo fdisk -l or dmesg with tail and grep)

if you want to get the label of the USB stick you can use sudo mlabel -i /dev/sdb1 -s :: and then extract the label using grep, awk or sed

https://help.ubuntu.com/community/RenameUSBDrive

if you want to get the UUID you can use sudo blkid /dev/sdb1

-> https://unix.stackexchange.com/questions/67464/how-to-get-uuid-for-a-usb-drive <-

https://serverfault.com/questions/3132/how-do-i-find-the-uuid-of-a-filesystem

See this Execute a Linux command in the c program and how to execute a command as root for executing linux commands like blkid in C

Community
  • 1
  • 1
ralf htp
  • 9,149
  • 4
  • 22
  • 34