你如何正常化Bash的文件路径? [英] How do you normalize a file path in Bash?
问题描述
我想改造 /富/酒吧/..
为 /富
有哪些做到这一点bash命令?
Is there a bash command which does this?
编辑:在我的实际情况下,该目录是存在的。
in my practical case, the directory does exist.
推荐答案
如果你想从终日啃食的路径,目录名称和基名是你的朋友,而真实路径的文件名的一部分,是太方便。
if you're wanting to chomp part of a filename from the path, "dirname" and "basename" are your friends, and "realpath" is handy too.
dirname /foo/bar/baz
# /foo/bar
basename /foo/bar/baz
# baz
dirname $( dirname /foo/bar/baz ))
# /foo
realpath ../foo
# ../foo: No such file or directory
realpath /tmp/../tmp/../tmp
# /tmp
修改
真实路径似乎不是标准的问题。
Realpath appears not to be standard issue.
您可以用储备标准得到最接近的是
The closest you can get with the stock standard is
readlink -f /path/here/..
真实路径似乎来自Debian的,并且不是的coreutils的一部分:
<一href=\"http://packages.debian.org/unstable/utils/realpath\">http://packages.debian.org/unstable/utils/realpath
这原是DWWW包的一部分。
Realpath appears to come from debian, and is not part of coreutils: http://packages.debian.org/unstable/utils/realpath Which was originally part of the DWWW package.
(也可在Gentoo的应用程序管理员/真实路径)
( also available on gentoo as app-admin/realpath )
readlink -m /path/there/../../
运行方式相同
realpath -s /path/here/../../
在于,实际存在的正常化它不需要的路径。
in that it doesn't need the path to actually exist to normalise it.
这篇关于你如何正常化Bash的文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!