2

My usb drive has id 580000911. It gets mounted as /media/ubuntu/580000911. I would like a udev rule to create a symbolic link to it like this /media/ubuntu/USBDRIVE. I created this udev rule file:

ACTION=="add", ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL}=="58??????", \
RUN+="rm /media/ubuntu/USBDRIVE", \
RUN+="ln -s /media/ubuntu/$env{ID_FS_LABEL} /media/ubuntu/USBDRIVE"
ACTION=="remove", ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL}=="58??????", \
RUN+="rm /media/ubuntu/USBDRIVE"

It does not work. I don't know if I am on the right track or not. Any suggestions?

10
  • I thought SYMLINK is meant for that? Aug 31 '17 at 22:14
  • I will see what I can learn about symlink. It seems like what I tried should work.
    – Bob
    Aug 31 '17 at 22:23
  • Have you tried using udevtest to debug the rule? Sep 1 '17 at 8:14
  • I see `\`, rules are oneliners so that should not be there Sep 1 '17 at 10:10
  • I use udevadm monitor to help write my scripts. Simply start it, then plug in and un-plug your USB stick. Copy and paste terminal output into your script. Sep 1 '17 at 10:15
3

This works:

ACTION=="add", ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL}=="58??????", RUN+="/bin/rm /media/ubuntu/USB", RUN+="/bin/ln -s /media/ubuntu/$env{ID_FS_LABEL} /media/ubuntu/USB"
ACTION=="remove", ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL}=="58??????", RUN+="/bin/rm /media/ubuntu/USB"

Apparently we have to use the complete pathname for any commands we want to run.

1
  • Would you please accept this answer as this what worked for you, so other users can find this solution easily. See tour
    – user.dz
    Sep 17 '17 at 9:02

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.