22

I can print current directory using pwd, but this gives me the path I navigated to get to where I am. I need to know which disk/partition current directory is on.

For example, if I create symlink user@pc:~$ ln -s /media/HD1 hard_disk and then navigate to ~/hard_disk and run pwd it will print /home/user/hard_disk.

I would like to get the actual path I'm currently on or better just the actual filesystem I'm currently on, which corresponds to one in df.

Rizhiy
  • 323
  • 1
  • 2
  • 7
  • 1
    What should the output be for a filesystem that does not live on a partition? What should the output be for a union filesystem that is the union of multiple different filesystems that may live on different partitions (or no partitions at all)? – Jörg W Mittag Apr 02 '19 at 13:55
  • Related https://unix.stackexchange.com/q/508420/255251 – Prvt_Yadav Apr 02 '19 at 14:07
  • Possible duplicate of [Determine what device a directory is located on](https://unix.stackexchange.com/questions/128471/determine-what-device-a-directory-is-located-on) or [How do I find on which physical device a folder is located?](https://unix.stackexchange.com/q/11311/22142) etc... plenty of duplicates but nobody wants to search. Nobody. – don_crissti Apr 02 '19 at 14:16
  • 1
    @don_crissti The duplicate answers do not mention `pwd -P`, but ok. – Kusalananda Apr 02 '19 at 16:02

2 Answers2

35

pwd -P will give you the physical directory you are in, i.e. the pathname of the current working directory with the symbolic links resolved.

Using df . would give you the df output for whatever partition the current directory is residing on.

Example (on an OpenBSD machine):

$ pwd
/usr/ports
$ pwd -P
/extra/ports
$ df .
Filesystem  512-blocks      Used     Avail Capacity  Mounted on
/dev/sd3a    103196440  55987080  42049540    57%    /extra

To parse out the mountpoint from this output, you may use something like

$ df -P . | sed -n '$s/[^%]*%[[:blank:]]*//p'
/extra

To parse out the filesystem device used, use

$ df -P . | sed -n '$s/[[:blank:]].*//p'
/dev/sd3a

I believe some Linux systems also supports

findmnt --target .

(where --target . can be replaced by -T .) or, for more terse output,

findmnt --output target --noheadings --target .

(where --noheadings may be replaced by -n, and --output target may be replaced by -o target) to get the mountpoint holding the filesystem that the current directory is located on.

Use --output source to get the mounted device node.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • *"To parse out the filesystem device used, use"* ...unless you're on a system that uses ZFS, at which point that will print the ZFS file system name. So you'd get something like `tank/data/whatever/someplace` back, where `tank` is the pool name. (At that point you could map it back to a pool, and from there to a set of candidate disks, but I'm pretty sure there's no direct way to map it back to "a" disk.) – user Apr 02 '19 at 12:46
1

As said by Ignacio here, you can use df -P file/goes/here | tail -1 | cut -d' ' -f 1 .