Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a script to recursively create symlinks in my home directory to my settings directory, to keep the files under version control. I would like it to skip files which are already symlinked via a parent directory. That is, if I have these files/directories:

~/foo/ -> ~/settings/foo/

, how do I check that ~/foo/bar and ~/settings/foo/bar are the same file?

Edit: D'oh, another few minutes of searching revealed the answer: readlink -f $path

share|improve this question
up vote 6 down vote accepted

If two files have the same deviceID and inode, they're the same file. The stat command line tool makes this easy to find

if [ "$(stat -c "%d:%i" FILE1)" == "$(stat -c "%d:%i" FILE2)" ]`


this works for symlinked files or hardlinked files.

share|improve this answer
To follow symbolic links, you need stat -L. – Gilles Oct 6 '10 at 20:13

Many shells have a -ef operator for the test builtin (or its synonym [) to test whether two paths point to the same existing file (following symbolic links). This includes bash, dash, pdksh, ksh88, ksh93 and zsh, but not POSIX sh. In bash, ksh or zsh, you can also use -ef in the [[ … ]] conditional construct.

if ! [ "$1" -ef "$2" ]; then # $1 and $2 are different files
share|improve this answer
According to my manpage, -ef does exactly the same thing as Rich Homolka's answer. – user13852 May 10 '13 at 14:51
@user13852 -ef has the advantage of being more portable (also slightly faster, but not so that you'd notice most of the time). For example it works on *BSD, OSX, BusyBox as well as Linux/Cygwin (GNU). It's also clearer, as it does exactly what is required. The only advantage I can see to calling stat is when you don't want to follow symlinks. – Gilles May 10 '13 at 14:59

For softlinks (created with ln -s) you can use realpath. This won't work for hardlinks though.

share|improve this answer

You must log in to answer this question.

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