Get mountpoint from filename

Dear Guru's

Given a full filename /a/b/c/d/file.txt how do i determine what part is the mount point ( say /a/b).

Cheers, Karel

Hi karelb,

For a path like your sample you can try:

echo "/a/b/c/d/file.txt" | awk -F"/" '{print "/" $2"/"$3}'
/a/b

Regards

cgkmal

thanks for your answer but that is not exactly what I am after.
To clarify more exactly what I want is the following:

Given an arbitrary full file name on a system I do not know, how do I determine the automount part of that filename.

The hard way would be:

  • strip the filename from the path
  • df -k the path
  • process the outcome and I have the mountpoint

I was hoping there would be standard command for that?

file=($(echo /a/b/c/d/file.txt | sed 's|/| |g'))
for i in ${file[@]}; do [[ $(df -h|grep $i) ]] && echo -e "mount point exist in\n `df -h|grep $i `" ; done

Indeed you will need to use the 'df' command. This is what I would do...

On Linux:

df -P $(dirname /a/b/c/d/file.txt) | awk '{c++} c==2 {print $NF}'

On Solaris:

df $(dirname /a/b/c/d/file.txt) | awk '{print $1}'

Hope this helps,
Mark.

df would be the right thing you are looking for.

btw, dirname perhaps could be omitted.

if you are under
/a/b/c/d/
df file.txt | awk....

will give you the mount point.

if you gave full path with file name:

df /a/b/c/d/file.txt |awk ...

will also give the mount point back.

Gents

Thanks for the excellent answers.
I will adopt the one-liners from Mark as they are more readable.

Cheers, Karel