是否有一个命令来检索给定相对路径的绝对路径?
例如,我想要$line包含dir ./etc/中每个文件的绝对路径
find ./ -type f | while read line; do
echo $line
done
是否有一个命令来检索给定相对路径的绝对路径?
例如,我想要$line包含dir ./etc/中每个文件的绝对路径
find ./ -type f | while read line; do
echo $line
done
当前回答
他们说,除了找到$PWD或(在bash中)找到~+更方便一些。
其他回答
蓝光:
cd $relative_path ; pwd
这里有一个POSIX兼容的解释(我认为),所以它应该在任何平台上工作。
当然,这是可编写脚本的,但我认为分解它可能会使一些人更容易理解/修改特定的用例。
你可以使用which, locate, find,全路径等等。
x = your_file_name
$ x="nvi"
文件容易指示符号链接
$ file -h `which $x`
/usr/local/bin/nvi: symbolic link to ../Cellar/nvi/1.81.6_5/bin/nvi
接下来,稍微改变一下输出,这样我们就得到了一个“完整的”相对路径。 在这个例子中,我们只需要去掉中间的部分。
注意大写Y和小写x。可能有更简洁的方法。
$ Y=$(file -h `which $x` | sed "s/$x: symbolic link to //")
$ echo $Y
/usr/local/bin/../Cellar/nvi/1.81.6_5/bin/nvi
使用dirname,我们只得到路径部分。CD给它,名字就会自己清理干净。
$ cd `dirname $Y` ; pwd
/usr/local/Cellar/nvi/1.81.6_5/bin
这将我们引向UNIX的老技巧,即通过在括号/子shell中执行所有操作来“幽灵巡视”目录。这将有效地将我们返回到当前目录。
为了完整起见,我们可以将实际的文件名粘回末尾。
Ls甚至确保绝对路径是有效的,这是额外的加分。
$ ( cd `dirname ${Y}` ; ls `pwd`/${x} )
/usr/local/Cellar/nvi/1.81.6_5/bin/nvi
所以/usr/local/bin/nvi实际上是/usr/local/Cellar/nvi/1.81.6_5/bin/nvi
快速“转换”路径的简化示例:
$ (cd /usr/local/bin/../Cellar/nvi/1.81.6_5/bin ; pwd)
/usr/local/Cellar/nvi/1.81.6_5/bin
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/file.html https://pubs.opengroup.org/onlinepubs/9699919799/utilities/dirname.html
你可以使用bash字符串替换任何相对路径$line:
line=$(echo ${line/#..\//`cd ..; pwd`\/})
line=$(echo ${line/#.\//`pwd`\/})
echo $line
基本的字符串前端替换遵循以下公式 ${/ #子字符串替换字符串} 在这里讨论得很好:https://www.tldp.org/LDP/abs/html/string-manipulation.html
当我们希望/是我们找到/替换的字符串的一部分时,\字符对/求反。
Realpath可能是最好的
但是…
最初的问题一开始就很混乱,没有一个很好的例子 与刚才提到的问题有关。
所选的答案实际上回答了给出的例子,但根本不是 题目中的问题。第一个命令是答案(is it 真的吗?我怀疑),没有'/'也可以。我看不见 第二个命令在做什么。
有几个问题是复杂的:
changing a relative pathname into an absolute one, whatever it denotes, possibly nothing. (Typically, if you issue a command such as touch foo/bar, the pathname foo/bar must exist for you, and possibly be used in computation, before the file is actually created.) there may be several absolute pathname that denote the same file (or potential file), notably because of symbolic links (symlinks) on the path, but possibly for other reasons (a device may be mounted twice as read-only). One may or may not want to resolve explicity such symlinks. getting to the end of a chain of symbolic links to a non-symlink file or name. This may or may not yield an absolute path name, depending on how it is done. And one may, or may not want to resolve it into an absolute pathname.
没有选项的命令readlink foo只在其 参数foo是一个符号链接,答案就是它的值 符号链接。后面没有其他链接。答案可能是相对路径: 无论符号链接参数的值是什么。
但是,readlink具有适用于所有对象的选项(-f -e或-m) 文件,并给出一个绝对路径名(没有符号链接的路径名) 由参数实际表示的文件。
这适用于任何不是符号链接的内容,尽管可能存在这种情况 希望使用绝对路径名而不解析中间路径 路径上的符号链接。这是通过命令realpath -s foo完成的
在符号链接参数的情况下,readlink及其选项将 再次解析参数的绝对路径上的所有符号链接,但是 这也将包括可能遇到的所有符号链接 在参数值之后。如果你想要一个 参数符号链接本身的绝对路径,而不是指向任何东西 它可能链接到。同样,如果foo是符号链接,realpath -s foo将 获取一个绝对路径,而不解析符号链接,包括符号链接 给出作为论据的。
如果没有-s选项,realpath的作用与 Readlink,除了简单地读取一个链接以及几个链接的值 其他的事情。我只是不清楚为什么readlink有它的 选项,显然造成了不受欢迎的冗余 realpath。
探索网络并不能说明更多,除了可能会有 跨系统的一些变化。
结论:realpath是最适合使用的命令 灵活性,至少对于这里要求的用途。
use:
find "$(pwd)"/ -type f
获取所有文件或
echo "$(pwd)/$line"
显示完整路径(如果相对路径关系到)
#! /bin/sh
echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
一些解释
这个脚本获取相对路径作为参数"$1" 然后我们得到该路径的dirname部分(你可以将dir或file传递给这个脚本): 然后我们将“$(dirname“$1”)cd到这个相对目录中,并通过运行pwd shell命令获得它的绝对路径 在此之后,我们将basename附加到绝对路径: 作为最后一步,我们重复它