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.

3
  • isn't mount will not solve your problem? Commented Jun 8, 2019 at 10:34
  • 1
    Possible duplicate of mount info for current directory Commented Jun 8, 2019 at 11:08
  • @roaima No, your edit was correct. Commented Jun 10, 2019 at 14:20

2 Answers 2

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.

2
  • df can be real pain to parse. If you have a sufficiently long device name (LVM) the output can be split across two lines. Commented Jun 9, 2019 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. Commented Jun 9, 2019 at 23:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.