4

How can I determine the address of a pen drive in Linux?

My task is to create a file on my pen drive dynamically - as soon as I insert the pen drive, the file should be created.

Gaff
  • 18,569
  • 15
  • 57
  • 68
R-The_Master
  • 161
  • 2

3 Answers3

3

I would recommend using udev.

Edit: Here's a similar question which was posted in 2007. http://www.linuxquestions.org/questions/linux-software-2/how-to-detect-programatically-if-a-usb-device-is-is-plugged-in-plugged-out-524568/

Daisetsu
  • 5,881
  • 4
  • 33
  • 44
2

In Ubuntu at least (meaning that the drive automounts), a drive's files can be found under /media/<drivelabel>. The drive label is either something random if you didn't set one or what you set it to.

digitxp
  • 14,432
  • 9
  • 56
  • 76
0

+1 for the UDEV Create an UDEV rule that runs a script, whenever a device with a specific UUID is inserted.

To determine the mountpoint of a device with e.g. uuid of "95738a33-589f-498d-8595-a81207f45dde", you can use the following ugly-but-working one-liner:

grep $(ls -lah /dev/disk/by-uuid/95738a33-589f-498d-8595-a81207f45dde | grep -o "sd[a-z]*[0-9]*") /etc/mtab |  awk '{print $2}'
  • I like that Idea....but the issue is that I just wanted the starting address of the drive in a variable like long x ...This is a variable in a C function....so how do i do that ?? – R-The_Master Dec 11 '10 at 16:44