63

我不确定这些路径是否重复。给定相对路径,如何使用 shell 脚本确定绝对路径?

例子:

relative path: /x/y/../../a/b/z/../c/d

absolute path: /a/b/c/d
4

7 回答 7

58

我在 unix 中遇到的最可靠的方法是readlink -f

$ readlink -f /x/y/../../a/b/z/../c/d
/a/b/c/d

几个警告:

  1. 这也有解决所有符号链接的副作用。这可能是可取的,也可能不是可取的,但通常是这样。
  2. readlink如果您引用不存在的目录,将给出空白结果。如果要支持不存在的路径,请readlink -m改用。不幸的是,这个选项在 ~2005 之前发布的 readlink 版本中不存在。
于 2010-10-28T17:18:28.530 回答
50

来自这个来源

#!/bin/bash

# Assume parameter passed in is a relative path to a directory.
# For brevity, we won't do argument type or length checking.

ABS_PATH=`cd "$1"; pwd` # double quotes for paths that contain spaces etc...
echo "Absolute path: $ABS_PATH"

你也可以做一个 Perl 单线,例如使用Cwd::abs_path

于 2010-10-28T17:06:17.043 回答
21

使用

# Directory
relative_dir="folder/subfolder/"
absolute_dir="$( cd "$relative_dir" && pwd )"

# File
relative_file="folder/subfolder/file"
absolute_file="$( cd "${relative_file%/*}" && pwd )"/"${relative_file##*/}"
  • ${relative_file%/*}结果与dirname "$relative_file"
  • ${relative_file##*/}结果与basename "$relative_file"

注意事项:不解析符号链接(即不规范化路径)=> 如果您使用符号链接,可能无法区分所有重复项。


使用realpath

命令realpath完成这项工作。另一种方法是使用readlink -e(或readlink -f)。但是realpath默认情况下并不经常安装。如果您不能确定realpathreadlink存在,您可以使用 perl 替换它(见下文)。


使用

realpath如果您的系统中不可用,则Steven Kramer建议使用 shell 别名:

$ alias realpath="perl -MCwd -e 'print Cwd::realpath(\$ARGV[0]),qq<\n>'"
$ realpath path/folder/file
/home/user/absolute/path/folder/file

或者如果您更喜欢直接使用 perl:

$ perl -MCwd -e 'print Cwd::realpath($ARGV[0]),qq<\n>' path/folder/file
/home/user/absolute/path/folder/file

这个单行 perl 命令使用Cwd::realpath. 实际上有三个 perl 函数。它们采用单个参数并返回绝对路径名。以下详细信息来自文档Perl5 > Core modules > Cwd

  • abs_path()使用与 相同的算法getcwd()。符号链接和相对路径组件(...)被解析为返回规范路径名,就像realpath.

    use Cwd 'abs_path';
    my $abs_path = abs_path($file);
    
  • realpath()是同义词abs_path()

    use Cwd 'realpath';
    my $abs_path = realpath($file);
    
  • fast_abs_path()是一个更危险但可能更快的版本abs_path()

    use Cwd 'fast_abs_path';
    my $abs_path = fast_abs_path($file);
    

这些函数仅在请求时导出 => 因此用于Cwd避免arielf指出的“未定义子例程”错误。如果要导入所有这三个函数,可以使用一行:use Cwd

use Cwd qw(abs_path realpath fast_abs_path);
于 2013-10-22T08:40:45.683 回答
19

看看“真实路径”。

$ realpath

usage: realpath [-q] path [...]

$ realpath ../../../../../

/data/home
于 2010-10-28T17:25:24.533 回答
1

由于这些年来我遇到过很多次,而这一次我需要一个可以在 OSX 和 linux 上使用的纯 bash 便携版本,我继续写了一个:

活版在这里:

https://github.com/keen99/shell-functions/tree/master/resolve_path

但为了如此,这是当前版本(我觉得它已经过很好的测试......但我愿意接受反馈!)

让它适用于普通的 bourne shell (sh) 可能并不难,但我没有尝试......我太喜欢 $FUNCNAME 了。:)

#!/bin/bash

resolve_path() {
    #I'm bash only, please!
    # usage:  resolve_path <a file or directory> 
    # follows symlinks and relative paths, returns a full real path
    #
    local owd="$PWD"
    #echo "$FUNCNAME for $1" >&2
    local opath="$1"
    local npath=""
    local obase=$(basename "$opath")
    local odir=$(dirname "$opath")
    if [[ -L "$opath" ]]
    then
    #it's a link.
    #file or directory, we want to cd into it's dir
        cd $odir
    #then extract where the link points.
        npath=$(readlink "$obase")
        #have to -L BEFORE we -f, because -f includes -L :(
        if [[ -L $npath ]]
         then
        #the link points to another symlink, so go follow that.
            resolve_path "$npath"
            #and finish out early, we're done.
            return $?
            #done
        elif [[ -f $npath ]]
        #the link points to a file.
         then
            #get the dir for the new file
            nbase=$(basename $npath)
            npath=$(dirname $npath)
            cd "$npath"
            ndir=$(pwd -P)
            retval=0
            #done
        elif [[ -d $npath ]]
         then
        #the link points to a directory.
            cd "$npath"
            ndir=$(pwd -P)
            retval=0
            #done
        else
            echo "$FUNCNAME: ERROR: unknown condition inside link!!" >&2
            echo "opath [[ $opath ]]" >&2
            echo "npath [[ $npath ]]" >&2
            return 1
        fi
    else
        if ! [[ -e "$opath" ]]
         then
            echo "$FUNCNAME: $opath: No such file or directory" >&2
            return 1
            #and break early
        elif [[ -d "$opath" ]]
         then 
            cd "$opath"
            ndir=$(pwd -P)
            retval=0
            #done
        elif [[ -f "$opath" ]]
         then
            cd $odir
            ndir=$(pwd -P)
            nbase=$(basename "$opath")
            retval=0
            #done
        else
            echo "$FUNCNAME: ERROR: unknown condition outside link!!" >&2
            echo "opath [[ $opath ]]" >&2
            return 1
        fi
    fi
    #now assemble our output
    echo -n "$ndir"
    if [[ "x${nbase:=}" != "x" ]]
     then
        echo "/$nbase"
    else 
        echo
    fi
    #now return to where we were
    cd "$owd"
    return $retval
}

这是一个经典的例子,感谢 brew:

%% ls -l `which mvn`
lrwxr-xr-x  1 draistrick  502  29 Dec 17 10:50 /usr/local/bin/mvn@ -> ../Cellar/maven/3.2.3/bin/mvn

使用此函数,它将返回 -real- 路径:

%% cat test.sh
#!/bin/bash
. resolve_path.inc
echo
echo "relative symlinked path:"
which mvn
echo
echo "and the real path:"
resolve_path `which mvn`


%% test.sh

relative symlinked path:
/usr/local/bin/mvn

and the real path:
/usr/local/Cellar/maven/3.2.3/libexec/bin/mvn
于 2014-12-24T21:01:52.207 回答
0

我想使用realpath但它在我的系统(macOS)上不可用,所以我想出了这个脚本:

#!/bin/sh

# NAME
#   absolute_path.sh -- convert relative path into absolute path
#
# SYNOPSYS
#   absolute_path.sh ../relative/path/to/file

echo "$(cd $(dirname $1); pwd)/$(basename $1)"

例子:

./absolute_path.sh ../styles/academy-of-management-review.csl 
/Users/doej/GitHub/styles/academy-of-management-review.csl
于 2020-05-05T09:08:18.610 回答
-1

可能这有帮助:

$path = "~user/dir/../file" 
$resolvedPath = glob($path); #   (To resolve paths with '~')
# Since glob does not resolve relative path, we use abs_path 
$absPath      = abs_path($path);
于 2012-02-02T08:26:16.327 回答