是否有一种简单的方法可以打印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
当前回答
另一个Linux实用程序,它做这个工作:
fname <file>
其他回答
在类似的场景中,我从其他位置启动cshell脚本。为了设置脚本的正确绝对路径,让它只在指定的目录中运行,我使用了以下代码:
set script_dir = `pwd`/`dirname $0`
$0存储脚本执行的确切字符串。
例如,如果脚本像这样启动:$> ../../test/test.csh, $script_dir将包含/home/abc/sandbox/v1/
你可以使用fpn(完整路径名)脚本:
% pwd
/Users/adamatan/bins/scripts/fpn
% ls
LICENSE README.md fpn.py
% fpn *
/Users/adamatan/bins/scripts/fpn/LICENSE
/Users/adamatan/bins/scripts/fpn/README.md
/Users/adamatan/bins/scripts/fpn/fpn.py
fpn不是一个标准的Linux包,但它是一个免费开放的github项目,你可以在一分钟内设置好。
在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 / -samefile file.txt -print
会发现所有链接的文件与相同的索引节点号为file.txt
添加-xdev标志将避免find跨越设备边界(“挂载点”)。(但是,如果find不是在与file.txt相同的设备上的目录开始,这可能会导致什么也找不到)
请注意,find可以报告单个文件系统对象的多个路径,因为一个Inode可以通过多个目录条目链接,甚至可能使用不同的名称。例如:
find /bin -samefile /bin/gunzip -ls
将输出:
12845178 4 -rwxr-xr-x 2 root root 2251 feb 9 2012 /bin/uncompress
12845178 4 -rwxr-xr-x 2 root root 2251 feb 9 2012 /bin/gunzip