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

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

<命令>

dir> <command> file.txt  

应该打印

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

当前回答

这将为您提供文件的绝对路径:

find / -name file.txt 

其他回答

你可以把它保存在壳里。Rc或者直接放到控制台

function absolute_path { echo "$PWD/$1"; }
alias ap="absolute_path"

例子:

ap somefile.txt

将输出

/home/user/somefile.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} \'
}

除了"readlink -f",还有一个常用的命令:

$find /the/long/path/but/I/can/use/TAB/to/auto/it/to/ -name myfile /the/long/path/but/I/can/use/TAB/to/auto/it/to/myfile $

这也给出了控制台的完整路径和文件名

跑题:这种方法只给出相对链接,而不是绝对链接。readlink -f命令是正确的。

这将为您提供文件的绝对路径:

find / -name file.txt 

我发现最简单的方法是:

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

这对我来说很有效。