Bash: 消除冗余路径组件?
Bash: eliminating redundant path components?
如何消除路径中的冗余组件?
比如我要改造
/foo/../foo/bar
到
/foo/bar
使用gnu realpath
:
p='/foo/../foo/bar'
realpath -m "$p"
输出:
/foo/bar
根据realpath --help
:
-m, --canonicalize-missing no components of the path need exist
你也可以使用更常用的readlink
(感谢@pjh):
readlink -m "$p"
您可以通过类似以下内容的管道:sed 's-/../foo/-/-g' 替换路径名中的 up/down 引用。
如何消除路径中的冗余组件?
比如我要改造
/foo/../foo/bar
到
/foo/bar
使用gnu realpath
:
p='/foo/../foo/bar'
realpath -m "$p"
输出:
/foo/bar
根据realpath --help
:
-m, --canonicalize-missing no components of the path need exist
你也可以使用更常用的readlink
(感谢@pjh):
readlink -m "$p"
您可以通过类似以下内容的管道:sed 's-/../foo/-/-g' 替换路径名中的 up/down 引用。