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

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

<命令>

dir> <command> file.txt  

应该打印

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

当前回答

在类似的场景中,我从其他位置启动cshell脚本。为了设置脚本的正确绝对路径,让它只在指定的目录中运行,我使用了以下代码:

set script_dir = `pwd`/`dirname $0`

$0存储脚本执行的确切字符串。

例如,如果脚本像这样启动:$> ../../test/test.csh, $script_dir将包含/home/abc/sandbox/v1/

其他回答

如果你和文件在同一个目录:

ls "`pwd`/file.txt"

用目标文件名替换file.txt。

除了"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命令是正确的。

fp () {
PHYS_DIR=`pwd -P`
RESULT=$PHYS_DIR/$1
echo $RESULT | pbcopy
echo $RESULT
}

将文本复制到剪贴板并在终端窗口上显示文本。

:)

(我从另一个堆栈溢出的答案复制了一些代码,但再也找不到答案了)

另一个Linux实用程序,它做这个工作:

fname <file>

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

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