如何检查 Bash 中的两条路径是否相等?

How to check if two paths are equal in Bash?

在 Bash 中检查两条路径是否相等的最佳方法是什么?例如,给定目录结构

~/
  Desktop/
    Downloads/ (symlink to ~/Downloads)
  Downloads/
    photo.png

假设当前目录是主目录,以下所有内容都是等价的:

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

是否有本机 Bash 方法来做到这一点?

原生bash方式:

pwd -P returns 物理目录,不考虑符号链接。

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

另一种方法是使用 cd -P,它将遵循符号链接但将您留在物理目录中:

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

如果您安装了 coreutils 8.15 或更高版本,您将有一个 realpath 命令可以完全规范化路径。它删除任何 ... 组件,使路径成为绝对路径,并解析所有符号链接。因此:

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

您可以使用 realpath。例如:

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

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

另请参阅此处的类似答案:bash/fish command to print absolute path to a file

当涉及其他因素时,基于解析符号链接的方法会失败。例如,绑定挂载。 (喜欢mount --bind /raid0/var-cache /var/cache

使用 find -samefile 是个不错的选择。这将比较文件系统和索引节点号。

-samefile 是 GNU 扩展。即使在 Linux 上,busybox find 也可能没有。 GNU 用户空间和 Linux 内核经常在一起,但你可以没有另一个,这个问题只被标记为 Linux 和 bash。)

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

例如在我的系统上:

$ find /var/tmp/EXP/cache/apt/archives/bash_4.3-14ubuntu1_amd64.deb  -samefile /var/cache/apt/archives/bash_4.3-14ubuntu1_amd64.deb 
/var/tmp/EXP/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

如果您想在最终路径组件中使用符号链接,您可以使用 find -L

$ 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
/var/cache/apt/archives/bash_4.3-14ubuntu1_amd64.deb

显然这适用于引用目录或任何类型文件的路径,而不仅仅是常规文件。它们都有 inode 编号。


用法:

$ samepath /var/cache/apt/  /var/tmp/EXP/cache/apt/  && echo true
true
$ ln -sf /var/cache/apt foobar
$ samepath foobar /var/tmp/EXP/cache/apt/  && echo true
true
samepath /var/tmp/EXP/cache/apt/ foobar   && echo true
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

因此 find -L 取消引用 -samefile 以及路径列表的符号链接。所以其中一个或两个都可以是符号链接。

Bash 的测试命令有一个 -ef 运算符用于此目的

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.