是否有一种简单的方法可以打印file.txt的完整路径?

file.txt = /nfs/an/disks/jj/home/dir/file.txt

<命令>

dir> <command> file.txt  

应该打印

/nfs/an/disks/jj/home/dir/file.txt

当前回答

这对我来说很有效。它不依赖于文件系统(取决于需要的利弊),所以它会很快;并且,它应该可以移植到大多数*NIX。它假设传递的字符串确实是相对于PWD,而不是其他目录。

function abspath () {
   echo $1 | awk '\
      # Root parent directory refs to the PWD for replacement below
      /^\.\.\// { sub("^", "./") } \
      # Replace the symbolic PWD refs with the absolute PWD \
      /^\.\//   { sub("^\.", ENVIRON["PWD"])} \
      # Print absolute paths \
      /^\//   {print} \'
}

其他回答

适用于Mac, Linux, *nix:

这将为您提供当前目录下所有文件的引用csv:

ls | xargs -I {} echo "$(pwd -P)/{}" | xargs | sed 's/ /","/g'

输出可以很容易地复制到python列表或任何类似的数据结构中。

在mac下面提到的行工作。不需要添加任何花哨的线条。

> pwd filename

获取文件的完整路径:

1)在包含你的文件的文件夹中打开你的终端,按键盘上的下列键:

CTRL + ALT + T

2)输入“pwd”(工作目录打印名称的首字母缩写):

your@device ~ $ pwd

这就是所有的朋友们!

我发现最简单的方法是:

for i in `ls`; do echo "`pwd`/$i"; done

这对我来说很有效。

您可以使用此功能。如果给出的文件名没有相对路径,则假定它在当前工作目录中:

abspath() { old=`pwd`;new=$(dirname "$1");if [ "$new" != "." ]; then cd $new; fi;file=`pwd`/$(basename "$1");cd $old;echo $file; }

用法:

$ abspath file.txt
/I/am/in/present/dir/file.txt

使用相对路径:

$ abspath ../../some/dir/some-file.txt
/I/am/in/some/dir/some-file.txt

文件名中有空格:

$ abspath "../../some/dir/another file.txt"
/I/am/in/some/dir/another file.txt