是否有一种简单的方法可以打印file.txt的完整路径?
file.txt = /nfs/an/disks/jj/home/dir/file.txt
<命令>
dir> <command> file.txt
应该打印
/nfs/an/disks/jj/home/dir/file.txt
是否有一种简单的方法可以打印file.txt的完整路径?
file.txt = /nfs/an/disks/jj/home/dir/file.txt
<命令>
dir> <command> file.txt
应该打印
/nfs/an/disks/jj/home/dir/file.txt
当前回答
在Mac OSX中,执行以下步骤:
CD到目标文件的目录。 输入下列任意一个终端命令。
Terminalls "`pwd`/file.txt"
echo $(pwd)/file.txt
用实际的文件名替换file.txt。 按回车键
其他回答
我喜欢已经给出的许多答案,但我发现这真的很有用,特别是在脚本中获得文件的完整路径,包括遵循符号链接和相对引用,例如。和. .
dirname `readlink -e relative/path/to/file`
它将返回从根路径开始的文件的完整路径。 这可以在脚本中使用,以便脚本知道它是从哪个路径运行的,这对于可能位于机器上任何位置的存储库克隆非常有用。
basePath=`dirname \`readlink -e $0\``
然后我可以在我的脚本中使用${basePath}变量来直接引用其他脚本。
希望这能有所帮助,
Dave
我发现最简单的方法是:
for i in `ls`; do echo "`pwd`/$i"; done
这对我来说很有效。
在mac下面提到的行工作。不需要添加任何花哨的线条。
> pwd filename
这很天真,但是我必须使它与POSIX兼容。需要进入文件目录的cd权限。
#!/bin/sh
if [ ${#} = 0 ]; then
echo "Error: 0 args. need 1" >&2
exit 1
fi
if [ -d ${1} ]; then
# Directory
base=$( cd ${1}; echo ${PWD##*/} )
dir=$( cd ${1}; echo ${PWD%${base}} )
if [ ${dir} = / ]; then
parentPath=${dir}
else
parentPath=${dir%/}
fi
if [ -z ${base} ] || [ -z ${parentPath} ]; then
if [ -n ${1} ]; then
fullPath=$( cd ${1}; echo ${PWD} )
else
echo "Error: unsupported scenario 1" >&2
exit 1
fi
fi
elif [ ${1%/*} = ${1} ]; then
if [ -f ./${1} ]; then
# File in current directory
base=$( echo ${1##*/} )
parentPath=$( echo ${PWD} )
else
echo "Error: unsupported scenario 2" >&2
exit 1
fi
elif [ -f ${1} ] && [ -d ${1%/*} ]; then
# File in directory
base=$( echo ${1##*/} )
parentPath=$( cd ${1%/*}; echo ${PWD} )
else
echo "Error: not file or directory" >&2
exit 1
fi
if [ ${parentPath} = / ]; then
fullPath=${fullPath:-${parentPath}${base}}
fi
fullPath=${fullPath:-${parentPath}/${base}}
if [ ! -e ${fullPath} ]; then
echo "Error: does not exist" >&2
exit 1
fi
echo ${fullPath}
这将为您提供文件的绝对路径:
find / -name file.txt