Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a *nix command or C api that will tell the type of the storage device given the path to the file (resolving the mounts, symlinks, etc)? I need to find out whether the file lives on a removable device.

share|improve this question
up vote 1 down vote accepted

I don't know whether find is sufficient to reveal the storage device:

find /path/to/your/file.xml -printf "%F"

from the manpage:

 %F     Type of the filesystem the file is on; this value can be used for -fstype.

Another idea is 'stat'

stat /home/stefan/.shosts  
  File: `/home/stefan/.shosts'
  Size: 12          Blocks: 8          IO Block: 4096   regular file
Device: 807h/2055d  Inode: 89647       Links: 1
Access: (0600/-rw-------)  Uid: ( 1000/  stefan)   Gid: ( 1000/  stefan)
Access: 2011-04-11 10:24:21.000000000 +0200
Modify: 2011-03-30 14:45:29.000000000 +0200
Change: 2011-03-30 14:46:03.000000000 +0200

There is a line 'Device:' but I don*t know what to do about it. Maybe you find out?

share|improve this answer
Device: is a 16 bit integer. The upper 8 bits are the device major and the lower 8 bits are the device minor. These can be compared to the major/minor numbers on the nodes in /dev to find the device name that the file is on. – Majenko Apr 12 '11 at 10:01
@Matt: On Linux, the major number is 12 bits and minor is 20 bits. – grawity Apr 12 '11 at 15:18
@grawity Not on my Linux it isn't – Majenko Apr 12 '11 at 17:01
@Matt: Well, I just did mknod foo c 4095 1048575 on a last year's kernel (2.6.32-linode). Can't speak for people stuck with 2.4, of course. – grawity Apr 12 '11 at 17:32
@grawity that may well be, but the Device: entry in stat is still 16 bits. stat[0] >> 8 = major. stat[0] & 0xFF = minor. Otherwise device 2055 (as in the example above) would be major 0 minor 2055... not quite right, is it? Linux may be capable of having device major and minors in bigger bits, but I don't think you can have a block device over minor 255. – Majenko Apr 12 '11 at 18:38

Some *nix implementations might have a cool tool, but it wouldn't be too hard to use mount(1) output to correlate a file and its filesystem:

mfe@inker:/home/mfe$ mount
/media/WINXP_PRO_SP1 on /dev/dsk/c4t0d0s2 read only/nosetuid/nodevices/noglobal/maplcase/joliet/traildot/dev=14c0002 on Thu Apr  7 07:39:29 2011

Giveaways in the mount name aside, any file I find under /media/WINXP_PRO_SP1 has go to be on the DVD-ROM.

share|improve this answer
Just a warning: mount(1) output is indended for humans. Do not parse it. Use /proc/self/mounts or /proc/self/mountinfo if the detection is to be put in a script. – grawity Apr 12 '11 at 12:16
Ah. We don't have that feature in Solaris. – mfe Apr 12 '11 at 14:49
There's still /etc/mtab, which uses the same format as /proc/self/mounts. – grawity Apr 12 '11 at 15:09
@grawity - /etc/mnttab, I'm sure you meant. – mfe Apr 12 '11 at 15:20

If hal is installed, you could e.g. scrape lshal's output.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .