我不确定这些路径是否重复。给定相对路径,如何使用 shell 脚本确定绝对路径?
示例:
relative path: /x/y/../../a/b/z/../c/d
absolute path: /a/b/c/d
我在unix中遇到的最可靠的方法是
readlink -f
:
$ readlink -f /x/y/../../a/b/z/../c/d
/a/b/c/d
一些注意事项:
readlink
将给出空白结果。如果您想支持不存在的路径,请使用 readlink -m
代替。不幸的是,这个选项在 2005 年之前发布的 readlink 版本中不存在。来自 http://www.robertpeaslee.com/index.php/converting-a-relative-path-to-an-absolute-path-in-bash/:
#!/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"
Cwd::abs_path
# 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
。如果您不能确定 realpath
或 readlink
是否存在,您可以使用 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
。实际上有 3 个 Perl 函数。它们采用单个参数并返回绝对路径名。以下详细信息来自文档 Perl5 > 核心模块 > 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);
看看“真实路径”。
$ realpath
usage: realpath [-q] path [...]
$ realpath ../../../../../
/data/home
由于这些年来我多次遇到这个问题,而这一次我需要一个可以在 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
使用此函数,它将返回-真实-路径:
%% 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
我想使用
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
这可能有帮助:
$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);