是否有一个命令来检索给定相对路径的绝对路径?

例如,我想要$line包含dir ./etc/中每个文件的绝对路径

find ./ -type f | while read line; do
   echo $line
done

当前回答

类似于@ernest-a的答案,但不影响$OLDPWD或定义一个新函数,您可以触发一个子shell (cd <path>;pwd)

$ pwd
/etc/apache2
$ cd ../cups 
$ cd -
/etc/apache2
$ (cd ~/..; pwd)
/Users
$ cd -
/etc/cups

其他回答

如果你想将一个包含相对路径的变量转换为绝对路径,这是可行的:

   dir=`cd "$dir"`

"cd"在不改变工作目录的情况下被回显,因为它是在子shell中执行的。

蓝光:

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

我发现Eugen Konkov的答案是最好的,因为它不需要安装任何程序。但是,对于不存在的目录,它将失败。

我写了一个函数,适用于不存在的目录:

function getRealPath()
{
    local -i traversals=0
    currentDir="$1"
    basename=''
    while :; do
        [[ "$currentDir" == '.' ]] && { echo "$1"; return 1; }
        [[ $traversals -eq 0 ]] && pwd=$(cd "$currentDir" 2>&1 && pwd) && { echo "$pwd/$basename"; return 0; }
        currentBasename="$(basename "$currentDir")"
        currentDir="$(dirname "$currentDir")"
        [[ "$currentBasename" == '..' ]] && (( ++traversals )) || { [[ traversals -gt 0 ]] && (( traversals-- )) || basename="$currentBasename/$basename"; }
    done
}

它通过使用dirname向上遍历直到cd成功,然后返回当前目录加上dirname删除的所有内容来解决不存在目录的问题。

我最喜欢的解决方案是@EugenKonkov的解决方案,因为它没有暗示其他实用程序的存在(coreutils包)。

但是对于相对路径“.”和“..”它失败了,所以这里有一个稍微改进的版本来处理这些特殊情况。

但是,如果用户没有cd到相对路径的父目录的权限,它仍然会失败。

#! /bin/sh

# Takes a path argument and returns it as an absolute path. 
# No-op if the path is already absolute.
function to-abs-path {
    local target="$1"

    if [ "$target" == "." ]; then
        echo "$(pwd)"
    elif [ "$target" == ".." ]; then
        echo "$(dirname "$(pwd)")"
    else
        echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
    fi
}

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是最适合使用的命令 灵活性,至少对于这里要求的用途。