How do I get the path to a DMG (disk image) file given the mounted volume path?

I tried hdiutil info -plist, but if I shift the DMG file elsewhere after mounting it, it doesn't update the DMG path. If I right click and select 'Get info' on the volume, I'm able to view the correct DMG path.


A somewhat pedestrian way would be to retrieve the name of your DMG file from the output of hdiutil and then search for it, e.g., via

hdiutil info -plist | grep dmg | sed 's/<string>\([^<]*\)<\/string>/\1/' | xargs basename | xargs mdfind -name

You may need to modify the grep dmg part to be smarter if you have several images mounted, or you loop through all DMG files reported by hdiutil, e.g.,

for F in `hdiutil info -plist | grep dmg | sed 's/<string>\([^<]*\)<\/string>/\1/'`
  do basename $F | xargs mdfind -name
done

However, none of the above takes your mount path as input. To do that, you could add a sed command in the pipe that extracts the paragraph of interest, e.g.,

hdiutil info -plist | tail -r | sed -n '/\/Volumes\/MOUNTNAME/,/dmg/p' | grep dmg | sed 's/<string>\([^<]*\)<\/string>/\1/' | xargs basename | xargs mdfind -name

Perhaps it is also easier to work on the direct output of hdiutil info, without the -plist flag.