1

Is there an alternative command that does the same thing as findmnt -T . -o TARGET |tail -n 1?

Example: If my current working directory is /media/username/HDD/subdir1/subdir2, that command would output /media/username/HDD/.

In Windows, the path to the highest directory of the current drive/partition is \ (also / works in cmd), because Windows does work with drive letters instead of "everything is a file".

Unix works more unified and modular, but how can one find to the mount point of a partition? If there is no shorter solution than findmnt -T . -o TARGET |tail -n 1, it is no problem. I just wanted to know whether there is a different way.

4

Looking at man findmnt I see a number of suggestions that seem to do what you want when finding the mount point for a filesystem:

findmnt --first-only --noheadings --output TARGET --target "$PWD"
/home

Or with less readability:

findmnt -fno TARGET -T "$PWD"
/home
2

If you just want to find it for yourself, not for later use in a variable. you won't find a shorter way than df .:

$ df . 
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/sda6      343650580 232263752  93860660  72% /home

That has extra details, of course, so can't be used to just save the device name to a variable directly. However, it's very easy to parse:

$ df . | grep -Po '^/\S+'
/dev/sda6
$ df . | awk '/^\//{print $1}'
/dev/sda6

But if you really want a single command to print it, then @roaima's findmnt will be the best.

  • df can be real pain to parse. If you have a sufficiently long device name (LVM) the output can be split across two lines. – roaima Jun 9 at 23:31
  • 1
    @roaima ooh, true, I hadn't even thought of LVM names. Still, the device should at least never have whitespace, so on GNU systems, the df . | grep -Po '^/\S+' should work. But yeah, findmt is a safer choice. I just like df . since it's so short and perfectly adequate for human consumption. – terdon Jun 9 at 23:58

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.