是否有一种简单的方法可以打印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
当前回答
你可以把它保存在壳里。Rc或者直接放到控制台
function absolute_path { echo "$PWD/$1"; }
alias ap="absolute_path"
例子:
ap somefile.txt
将输出
/home/user/somefile.txt
其他回答
find $PWD -type f | grep "filename"
or
find $PWD -type f -name "*filename*"
您可以使用此功能。如果给出的文件名没有相对路径,则假定它在当前工作目录中:
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
另一个Linux实用程序,它做这个工作:
fname <file>
除了"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
}
将文本复制到剪贴板并在终端窗口上显示文本。
:)
(我从另一个堆栈溢出的答案复制了一些代码,但再也找不到答案了)