8

What is the best way to find out file system information about a file.

For example, if I have a file

/media/xyz/path/to/file.ext

And /etc/fstab contains

//server1/Share1 /media/xyz cifs option1=value1,...

How can I determine that the file is on a Samba share, that it has the local path /path/to/file.ext and with what options the file system was mounted?

Is parsing the output of mount a portable solution? Or parse findmnt? Is there a POSIX-compliant way to achieve that?

  • df /path/to/file should show the partition or mount. – thrig Feb 12 '16 at 17:30
  • Thanks @thrig, that makes the first step. What would be the best way to get info like mount options from there? – kba stands with Monica Feb 12 '16 at 17:40
  • On Linux, I can parse /proc/mounts. Is there a BSD/OSX compatible way to achieve the same? – kba stands with Monica Feb 12 '16 at 17:57
  • just by looking at files? you can't! that is the beauty if unix-like operating systems, applications can access them by using path irrespective of their physical location – Edward Torvalds Feb 17 '16 at 10:50
  • @edwardtorvalds No, not just by looking at files, that's why I asked about parsing mount etc. for getting filesystem and options etc. I'm still not sure about the best way to find this information in *BSD and OSX. I haven't had time to work on the library that requires this but will feedback once I do. From what I saw, mount -P seems pretty reliable across Linux/BSD. – kba stands with Monica Feb 17 '16 at 11:10
1

stat(1) is a portable way to find out some of these information, e.g.:

stat --file-system --format="%T" /media/xyz/path/to/file.ext

Everything else is very OS-specific, on recent Linux systems you can easily use findmnt --output in a script.

  • stat is also os-specific. – don_crissti Mar 10 '16 at 9:47
0

if I understood your need correctly, and combining bits from previous answers, the following might be what you need. define in script (for example, in your .bashrc)-

findpart ()
{
df -h $1|sed 1d|cut -d" " -f1|xargs findmnt -n
}

and then you can run it as needed

findpart /foo/file.ext
  • findmnt doesn't need all that stuff, it can do this all by itself – don_crissti Mar 10 '16 at 9:42
  • theoretically, yes. in practice, i could not get -T to work on centos 6, whereas this solution does. – Gil Shabtai May 9 '16 at 11:37

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.