Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What's the best way to check if two directories belong to the same filesystem?

Acceptable answers: bash, python, C/C++.

share|improve this question
    
If you want python/C++ answers you're on the wrong site –  Michael Mrozek Jul 29 '12 at 18:29
    
Good point - I should write "python,C/C++ are acceptable". –  Grzegorz Wierzowiecki Jul 29 '12 at 19:10
    
@MichaelMrozek remember that C API questions are on-topic: meta.unix.stackexchange.com/questions/314/… –  Grzegorz Wierzowiecki Jul 30 '12 at 9:44

2 Answers 2

up vote 7 down vote accepted

It can be done by comparing device numbers.

In a shell script on Linux it can be done with stat:

stat -c "%d" /path  # returns the decimal device number 

In python:

os.lstat('/path...').st_dev

or

os.stat('/path...').st_dev
share|improve this answer

The standard command df shows on what filesystem the specified file(s) is located.

if df -P -- "$1" "$2" | awk 'NR==2 {dev1=$1} NR==3 {exit($1!=dev1)}'; then
  echo "$1 and $2 are on the same filesystem"
else
  echo "$1 and $2 are on different filesystems"
fi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.