Your problem is actually locating the mounted device containing a specific file; sycamorex and catkin already answered that quite well.
I've had to solve a situation where the user should be able to select any USB storage device,
whether or not it is mounted.
This is also easy to solve, without elevated privileges; you can find this out as a normal user without root access.
In case somebody reading this topic happens to need it, here's the code I've used:
Code:
#!/bin/bash
Device=()
Mounts=()
Name=()
Size=()
for node in /dev/sd[a-z] ; do
dev="${node#/dev/}"
removable=$[ `cat /sys/block/$dev/removable` -0 ]
if [ $removable -eq 1 ]; then
Device=("${Device[@]}" "$node")
Mounts=("${Mounts[@]}" `grep -ce "^$node" /proc/mounts`)
Name=("${Name[@]}" "`cat /sys/block/$dev/device/vendor` `cat /sys/block/$dev/device/model`")
Size=("${Size[@]}" $[(`cat /sys/block/$dev/size`-0)/2])
fi
done
i=0
while [ $i -lt ${#Device[@]} ]; do
d=${Device[i]}
m=${Mounts[i]}
n=${Name[i]}
s=${Size[i]}
i=$[i + 1]
echo "$d:"
echo " Name: $n"
echo " Size: $s kB"
case $m in
0) echo " Mounted: No" ;;
1) echo " Mounted: Yes, 1 partition" ;;
*) echo " Mounted: Yes, $m partitions" ;;
esac
echo ""
done
First part will fill in four parallel arrays: Device, Mounts, Name, and Size.
"${Device[0]}" is the device node for the first device,
${Mounts[1]} contains the number of partitions mounted on the second device, zero if none,
"${Name[0]}" is the vendor and model name for the first device, and
${Size[1]} is the size of the second device in kilobytes (1024 byte blocks).
The number of devices found is of course
${#Device[@]}.
The second part just outputs the list. Replace it with your own selection routines. I normally list one device per line, identified by a letter, and let the user select one or more devices by specifying their letters. Works quite reliably.
Hope this helps someone,
Nominal Animal