是否有一种简单的方法可以打印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
当前回答
获取文件的完整路径:
1)在包含你的文件的文件夹中打开你的终端,按键盘上的下列键:
CTRL + ALT + T
2)输入“pwd”(工作目录打印名称的首字母缩写):
your@device ~ $ pwd
这就是所有的朋友们!
其他回答
我猜你用的是Linux。
我在coreutils 8.15中找到了一个名为realpath的实用程序。
realpath -s file.txt
/data/ail_data/transformed_binaries/coreutils/test_folder_realpath/file.txt
由于问题是关于如何获取文件的完整/绝对路径,而不是关于如何获取符号链接的目标,请使用-s或——no-symlinks,这意味着不要展开符号链接。
根据@styrofoam-fly和@arch-standton注释,realpath本身并不检查文件是否存在,为了解决这个问题,添加e参数:realpath -e file
适用于Mac, Linux, *nix:
这将为您提供当前目录下所有文件的引用csv:
ls | xargs -I {} echo "$(pwd -P)/{}" | xargs | sed 's/ /","/g'
输出可以很容易地复制到python列表或任何类似的数据结构中。
您可以使用此功能。如果给出的文件名没有相对路径,则假定它在当前工作目录中:
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
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
这很天真,但是我必须使它与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}