Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

In a generic, modern unix environment (say, GNU/Linux, GNU/Solaris, or Mac OS X), is there a good way to determine which mountpoint and filesystem-type a particular absolute file path is on?

I suppose I could execute the mount command and manually parse the output of that and string-compare it with my file path, but before I do that I'm wondering if there's a more elegant way.

I'm developing a BASH script that makes use of extended attributes, and want to make it Do The Right Thing (to the small extent that it is possible) for a variety of filesystems and host environments.

share|improve this question

6 Answers 6

up vote 13 down vote accepted

The command df(1) takes one or more arguments and will return the mountpoint and device on which that file or directory exists, as well as usage information. You can then use the path or device to look up the filesystem type in the output of mount -v or similar.

Unfortunately, the output format of both df and mount are system-dependent; there is no apparent standard, at least as I can see between Solaris, NetBSD and Mac OS X.

share|improve this answer

You could use stat. The command stat --printf '%d' filename.txt will return the device number as hex/decimal.

share|improve this answer
    
So, How to find the device name base on that ? –  warl0ck Sep 5 '12 at 9:51
    
You need to go though all the device files in /dev/ and look up one with the same minor number as stat reported. –  Wiesław Herr Feb 22 '13 at 12:47

For just a specific file it's as easy as

df -T "${FILE}" | awk '{print $2}' | tail -n1
share|improve this answer

Hm. For the mount point, you can go up the hierarchy until the st_dev changes (then you have just crossed over a mount boundary); there's GNU stat for bash scripts; however, I don't know how you can guess the filesystem type without parsing /proc/mounts or by trial and error (i.e. handle failures after setting extended attributes)

share|improve this answer

One gotcha with using df is that if the device name in the output is long it's line will wrap so you can't just grab the last line. Instead strip off the first line and then grab the new first line and then print the first field:

#!/usr/bin/env bash

path=$1
curdir=$(pwd)
cd $path
df . | tail -n +2 | head -1 | awk '{print $1}'
cd $curdir
share|improve this answer
2  
Avoid this gotcha by using 'df -P' to get output in POSIX format and without line-breaking. –  MikeyB Dec 22 '09 at 5:01

From http://stackoverflow.com/questions/2167558/give-the-mount-point-of-a-path:

 df -P $path  | tail -1 | awk '{ print $NF}'

works everywhere I have tested, for both *BSDs and sysVs, and for wacky automounted directories. I'd be delighted to hear of a case where it fails.

share|improve this answer
1  
The suggested code df -P $path | tail -1 | awk '{ print $NF}' fails on all versions of Solaris I have tried (2.5.1, 8, 9, and 10) because Solaris’ “df” does not support the “-P” option. –  Peter John Acklam Nov 16 '11 at 9:23
    
@Peter: I am less delighted than I thought I would be. But it is interesting to know that the problem is nontrivial. I think the right things is to write a command in a scripting language whose library has solved the problem properly, e.g., Python has the os.path.splitunc() function that gives the mount point and which I assume works on Solaris. –  Charles Stewart Nov 17 '11 at 11:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.