【发布时间】:2015-08-06 11:10:33
【问题描述】:
我正在寻找一种方法来规范化 bash 中的任意文件路径,将 . 和 .. 解析为它们的绝对值。例子:
> cd /foo/bar
> normalize_path "."
/foo/bar
> normalize_path "../baz"
/foo/baz
> normalize_path "../../folder/that/doesnt/exist/"
/folder/that/doesnt/exist
请注意在上面的示例中,normalize_path 如何返回一个值即使路径不对应于真实的文件夹或文件。我在 StackOverflow(example 1、example 2)上看到的关于绝对或规范化路径的所有先前问题都假定路径指向真实文件并使用 realpath 或 pwd 对其进行规范化,但这不起作用对于我的用例。
换句话说,我正在寻找一个 bash 函数,它纯粹将路径作为字符串进行操作,实际上并不查看文件系统,类似于 Java 中的 File.getCanonicalPath。
【问题讨论】:
-
必须单独在
bash中还是可以使用readlink -m your_imaginary_path_here或realpath -m your_imaginary_path_here? -
@rickhg12hs:我正在寻找可以在 OS X 上运行的东西。