是否有一个命令来检索给定相对路径的绝对路径?
例如,我想要$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
当前回答
恕我直言,最好的解决方案是在这里发布的:https://stackoverflow.com/a/3373298/9724628。
它确实需要python才能工作,但它似乎涵盖了所有或大部分边缘情况,是非常可移植的解决方案。
通过解析符号链接:
python -c "import os,sys; print(os.path.realpath(sys.argv[1]))" path/to/file
或者没有它:
python -c "import os,sys; print(os.path.abspath(sys.argv[1]))" path/to/file
其他回答
echo "mydir/doc/ mydir/usoe ./mydir/usm" | awk '{ split($0,array," "); for(i in array){ system("cd "array[i]" && echo $PWD") } }'
这是来自所有其他解决方案的链式解决方案,例如,当realpath失败时,要么是因为没有安装它,要么是因为它带着错误代码退出,那么,将尝试下一个解决方案,直到它获得正确的路径。
#!/bin/bash
function getabsolutepath() {
local target;
local changedir;
local basedir;
local firstattempt;
target="${1}";
if [ "$target" == "." ];
then
printf "%s" "$(pwd)";
elif [ "$target" == ".." ];
then
printf "%s" "$(dirname "$(pwd)")";
else
changedir="$(dirname "${target}")" && basedir="$(basename "${target}")" && firstattempt="$(cd "${changedir}" && pwd)" && printf "%s/%s" "${firstattempt}" "${basedir}" && return 0;
firstattempt="$(readlink -f "${target}")" && printf "%s" "${firstattempt}" && return 0;
firstattempt="$(realpath "${target}")" && printf "%s" "${firstattempt}" && return 0;
# If everything fails... TRHOW PYTHON ON IT!!!
local fullpath;
local pythoninterpreter;
local pythonexecutables;
local pythonlocations;
pythoninterpreter="python";
declare -a pythonlocations=("/usr/bin" "/bin");
declare -a pythonexecutables=("python" "python2" "python3");
for path in "${pythonlocations[@]}";
do
for executable in "${pythonexecutables[@]}";
do
fullpath="${path}/${executable}";
if [[ -f "${fullpath}" ]];
then
# printf "Found ${fullpath}\\n";
pythoninterpreter="${fullpath}";
break;
fi;
done;
if [[ "${pythoninterpreter}" != "python" ]];
then
# printf "Breaking... ${pythoninterpreter}\\n"
break;
fi;
done;
firstattempt="$(${pythoninterpreter} -c "import os, sys; print( os.path.abspath( sys.argv[1] ) );" "${target}")" && printf "%s" "${firstattempt}" && return 0;
# printf "Error: Could not determine the absolute path!\\n";
return 1;
fi
}
printf "\\nResults:\\n%s\\nExit: %s\\n" "$(getabsolutepath "./asdfasdf/ asdfasdf")" "${?}"
#! /bin/sh
echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
一些解释
这个脚本获取相对路径作为参数"$1" 然后我们得到该路径的dirname部分(你可以将dir或file传递给这个脚本): 然后我们将“$(dirname“$1”)cd到这个相对目录中,并通过运行pwd shell命令获得它的绝对路径 在此之后,我们将basename附加到绝对路径: 作为最后一步,我们重复它
恕我直言,最好的解决方案是在这里发布的:https://stackoverflow.com/a/3373298/9724628。
它确实需要python才能工作,但它似乎涵盖了所有或大部分边缘情况,是非常可移植的解决方案。
通过解析符号链接:
python -c "import os,sys; print(os.path.realpath(sys.argv[1]))" path/to/file
或者没有它:
python -c "import os,sys; print(os.path.abspath(sys.argv[1]))" path/to/file
你可以使用bash字符串替换任何相对路径$line:
line=$(echo ${line/#..\//`cd ..; pwd`\/})
line=$(echo ${line/#.\//`pwd`\/})
echo $line
基本的字符串前端替换遵循以下公式 ${/ #子字符串替换字符串} 在这里讨论得很好:https://www.tldp.org/LDP/abs/html/string-manipulation.html
当我们希望/是我们找到/替换的字符串的一部分时,\字符对/求反。