2

I need to generate a string for another application to locate my file. I need to know how to access this file dynamically.

customfile=/dev/sda1:/images/myfs.squashfs

Let's say I can stat some/file/here.txt. What command do I use to determine it's block device, and it's absolute path from that device?

Also, how do I then determine the absolute path to here.txt from the device?

Paul Knopf
  • 1,231
  • You can get the device associated with the file with "df --output=source | tail -1". If logical volumes are involved, more steps may be required. – Raman Sailopal Sep 27 '17 at 09:22

1 Answers1

3

You can find on which device your file is with df. Select the output columns with --output option. Note that df outputs the device which contains the filesystem. If you use LVM, LUKS, mdraid etc, the source column will contain the path of the logical device.

df --output=source $path |tail -1

From target column you can get the mount point of the device:

df --output=target $path |tail -1

To get path relative to the root of the mounted device, remove the mount point from the beginning of the path. Using shell parameter expansion:

echo ${path#$(df $path --output=target |tail -1)}
sebasth
  • 14,872