How can I check if
/my/dir is on the same partition as
This is for integration within a script. Bind mounts should be handled correctly. POSIX compatible solutions are welcome.
The following command gives a unique name for the mount point containing the file
This works on any POSIX system. The
Or, to save a couple of process invocations:
A few operating systems can have spaces in volume names. There's no completely reliable way of parsing the
Under the hood, you can identify the filesystem containing a file by the
If Perl is available, you can use
and to do the comparison:
Note that there are some corner cases where the desired result is not clear. For example, with Linux's bind mounts, after
If the files are directories and you can write to them, another method is to create a temporary file and attempt to make a hard link. This one reports a negative result across Linux bind mounts.
Works with any number of paths.
You can check this with stat:
Shows you the device number and where your directory was mounted.
The best foolproof solution available in POSIX is the comparison of the files' device IDs provided by the stat(2) function.
but the "POSIX way" is to use a C program like:
which source code is as follows:
If both files' device IDs are equal, they are hosted on the same filesystem, in which case, the commands above return 0 (another value otherwise). Check with
This works well with bind mounts, but probably won't with network mounts.