How to check if two paths are equal in Bash?

What's the best way to check if two paths are equal in Bash? For example, given the directory structure

    Downloads/ (symlink to ~/Downloads)

and assuming that the current directory is the home directory, all of the following would be equivalent:

./                    and ~
~/Desktop             and /home/you/Desktop
./Downloads           and ~/Desktop/Downloads
./Downloads/photo.png and ~/Downloads/photo.png

Is there a native Bash way to do this?


Bash's test commands have a -ef operator for this purpose

if [[ ./ -ef ~ ]]; then ...

if [[ ~/Desktop -ef /home/you/Desktop ]]; then ...


$ help test | grep -e -ef
      FILE1 -ef FILE2  True if file1 is a hard link to file2.

You can use realpath. For example:

realpath ~/file.txt
Result: /home/testing/file.txt

realpath ./file.txt
Result: /home/testing/file.txt

Also take a look at a similar answer here: bash/fish command to print absolute path to a file

If you have coreutils 8.15 or later installed, you have a realpath command that fully normalizes a path. It removes any . and .. components, makes the path absolute, and resolves all symlinks. Thus:

if [ "$(realpath "$path1")" = "$(realpath "$path2")" ]; then
   echo "Same!"

Native bash way:

pwd -P returns the physical directory irrespective of symlinks.

cd "$dir1"
real1=$(pwd -P)
cd "$dir2"
real2=$(pwd -P)
# compare real1 with real2

Another way is to use cd -P, which will follow symlinks but leave you in the physical directory:

cd -P "$dir1"
cd -P "$dir2"
# compare real1 with real2

Methods based on resolving symlinks fail when there are other factors involved. For example, bind mounts. (Like mount --bind /raid0/var-cache /var/cache

Using find -samefile is a good bet. That will compare filesystem and inode number.

-samefile is a GNU extension. Even on Linux, busybox find probably won't have it. GNU userspace and Linux kernel often go together, but you can have either without the other, and this question is only tagged Linux and bash.)

# params: two paths.  returns true if they both refer to the same file
samepath() {
    # test if find prints anything
    [[ -s "$(find -L "$1" -samefile "$2")" ]]  # as the last command inside the {}, its exit status is the function return value

e.g. on my system:

$ find /var/tmp/EXP/cache/apt/archives/bash_4.3-14ubuntu1_amd64.deb  -samefile /var/cache/apt/archives/bash_4.3-14ubuntu1_amd64.deb 

$ stat {/var/tmp/EXP,/var}/cache/apt/archives/bash_4.3-14ubuntu1_amd64.deb
  File: ‘/var/tmp/EXP/cache/apt/archives/bash_4.3-14ubuntu1_amd64.deb’
Device: 97ch/2428d      Inode: 2147747863  Links: 1

  File: ‘/var/cache/apt/archives/bash_4.3-14ubuntu1_amd64.deb’
Device: 97ch/2428d      Inode: 2147747863  Links: 1

You can use find -L for cases where you want to follow symlinks in the final path component:

$ ln -s   /var/cache/apt/archives/bash_4.3-14ubuntu1_amd64.deb foo
$ find    /var/cache/apt/archives/bash_4.3-14ubuntu1_amd64.deb  -samefile foo
$ find -L /var/cache/apt/archives/bash_4.3-14ubuntu1_amd64.deb  -samefile foo

Obviously this works for paths which refer to directories or any type of file, not just regular files. They all have inode numbers.


$ samepath /var/cache/apt/  /var/tmp/EXP/cache/apt/  && echo true
$ ln -sf /var/cache/apt foobar
$ samepath foobar /var/tmp/EXP/cache/apt/  && echo true
samepath /var/tmp/EXP/cache/apt/ foobar   && echo true
samepath foobar   && echo true   # doesn't return true when find has an error, since the find output is empty.
find: `': No such file or directory

So find -L dereferences symlinks for -samefile, as well as for the list of paths. So either or both can be symlinks.

Need Your Help

Fit as much on the x axis as possible in chart control

c# winforms charts

I have a chart that has times on X axes. How can I remove the gaps in between the points on the X axes? I think that it might be called interval or scale or something but I'm not sure where in the

Android notification on lock screen

java android notifications

Is it possible to show notification on lock screen with API19? It shows in API23 but not in 19. Ive tried to search for a solution but havnt come across any answer i could use.