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


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.

  • 5
    To follow symbolic links, you need stat -L. – Gilles Oct 6 '10 at 20:13
  • Like @Gilles said, without the -L switch, you may get wrong results on some distributions – Olaseni Nov 23 '17 at 4:54

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
  • 1
    According to my manpage, -ef does exactly the same thing as Rich Homolka's answer. – user13852 May 10 '13 at 14:51
  • 2
    @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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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