150

Specifically: I did sudo mkdir /work, and would like to verify it indeed sits on my harddrive and not mapped to some other drive.

How do I check where this folder is physically located?

1

4 Answers 4

190

The df(1) command will tell you the device that a file or directory is on:

df /work

The first field has the device that the file or directory is on.

e.g.

$ df /root
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1              1043289    194300    795977  20% /

If the device is a logical volume, you will need to determine which block device(s) the logical volume is on. For this, you can use the lvs(8) command:

# df /usr
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/orthanc-usr
                       8256952   4578000   3259524  59% /usr
# lvs -o +devices /dev/mapper/orthanc-usr
  LV   VG      Attr   LSize Origin Snap%  Move Log Copy%  Convert Devices     
  usr  orthanc -wi-ao 8.00g                                       /dev/sda3(0)

The last column tells you that the logical volume usr in the volume group orthanc (/dev/mapper/orthanc-usr) is on the device /dev/sda3. Since a volume group can span multiple physical volumes, you may find that you have multiple devices listed.

Another type of logical block device is a md (Multiple Devices, and used to be called meta-disk I think) device, such as /dev/md2. To look at the components of a md device, you can use mdadm --detail or look in /proc/mdstat

# df /srv
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/md2             956626436 199340344 757286092  21% /srv
# mdadm --detail /dev/md2
...details elided...
    Number   Major   Minor   RaidDevice State
       0       8        3        0      active sync   /dev/sda3
       1       8       19        1      active sync   /dev/sdb3

You can see that /dev/md2 is on the /dev/sda3 and /dev/sdb3 devices.

There are other methods that block devices can be nested (fuse, loopback filesystems) that will have their own methods for determining the underlying block device, and you can even nest multiple layers so you have to work your way down. You'll have to take each case as it comes.

4
  • 4
    +1. Interesting. I didn't know df was so accomodating. Apr 14, 2011 at 10:34
  • OK, I did that and got /dev/mapper/fun-root as my Filesystem. Now what? (My computer's name is 'fun')
    – ripper234
    Apr 14, 2011 at 11:37
  • 1
    Then perhaps you are using LVM? lvdisplay or lvs will be your friend.
    – phunehehe
    Apr 14, 2011 at 13:38
  • 6
    I was so impressed with this answer that I used it as the basis for a command, "rawdev", that returns the underlying device(s) of a path or partition, even in cases where LVM and/or MD are nested. It's available on Github: github.com/BMDan/rawdev .
    – BMDan
    Mar 10, 2015 at 19:02
10

For a script, you can use:

$ df -P <pathname> | awk 'END{print $1}'

This is POSIX compatible.

1
  • Works most of the time, however if <pathname> is in a btrfs snapshot this reports '-'. Mar 16, 2018 at 18:06
6

In modern distributions of Ubuntu there's an additional layer (device mapper) between your file/directory and the device. /dev/mapper contains symbolic links pointing to the actual special devices. For example, trying on the current directory:

$ df . | grep '^/' | cut -d' ' -f1
/dev/mapper/kubuntu--vg-root

$ ls -l /dev/mapper/kubuntu--vg-root
lrwxrwxrwx 1 root root 7 Nov 22 18:02 /dev/mapper/kubuntu--vg-root -> ../dm-1

So to get the full path of device programmatically, you can use:

$ realpath $(df . | grep '^/' | cut -d' ' -f1)

Which is my case prints:

/dev/dm-1

realpath is part of GNU coreutils.

3
  • Nice, but when is the full path useful? df will still display the mapper path. Jan 2, 2019 at 7:27
  • @DanDascalescu fair point. It may be useful when/if you want to know the actual hardware device which is what I was looking for when hitting this question.
    – arielf
    Jan 30, 2019 at 23:58
  • 1
    I suppose you must be using a LUKS encrypted disk, as I get /dev/dm-n on those but the block device name (eg. /dev/sda3) for non-encrypted.
    – paradroid
    Jun 26, 2021 at 2:11
0
$ findmnt -no source -T /      # mountpoint
/dev/nvme0n1p2
$ findmnt -no source -T /usr   # directory under mountpoint
/dev/nvme0n1p2
$ COLUMNS=70 man findmnt | sed -E '/^ {7}-[noT]/,/^$/!d;s/^ {7}//'           
-n, --noheadings
    Do not print a header line.

-o, --output list
    Define output columns. See the --help output to get a
    list of the currently supported columns. The TARGET
    column contains tree formatting if the --list or --raw
    options are not specified.

-T, --target path
    Define the mount target. If path is not a mountpoint file
    or directory, then findmnt checks the path elements in
    reverse order to get the mountpoint (this feature is
    supported only when searching in kernel files and
    unsupported for --fstab). It’s recommended to use the
    option --mountpoint when checks of path elements are
    unwanted and path is a strictly specified mountpoint.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .