Linux 如何获取文件的完整路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5265702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get full path of a file?
提问by Jean
Is there an easy way I can print the full path of file.txt
?
有没有一种简单的方法可以打印 的完整路径file.txt
?
file.txt = /nfs/an/disks/jj/home/dir/file.txt
The <command>
这 <command>
dir> <command> file.txt
should print
应该打印
/nfs/an/disks/jj/home/dir/file.txt
回答by jcomeau_ictx
I knowthere's an easier way that this, but darned if I can find it...
我知道有一种更简单的方法,但如果我能找到它,那就太糟糕了......
jcomeau@intrepid:~$ python -c 'import os; print(os.path.abspath("cat.wav"))'
/home/jcomeau/cat.wav
jcomeau@intrepid:~$ ls $PWD/cat.wav
/home/jcomeau/cat.wav
回答by ZeRemz
The following usually does the trick:
以下通常可以解决问题:
echo $(cd $(dirname "") && pwd -P)/$(basename "")
回答by Andrew_1510
Beside "readlink -f" , another commonly used command:
除了 "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 $
$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 $
This also give the full path and file name at console
这也给出了控制台的完整路径和文件名
Off-topic: This method just gives relativelinks, not absolute. The readlink -f
command is the right one.
题外话:这个方法只给出相对链接,而不是绝对链接。该readlink -f
命令是正确的。
回答by Ackq
In a similar scenario, I'm launching a cshell script from some other location. For setting the correct absolute path of the script so that it runs in the designated directory only, I'm using the following code:
在类似的情况下,我从其他位置启动 cshell 脚本。为了设置脚本的正确绝对路径,使其仅在指定目录中运行,我使用以下代码:
set script_dir = `pwd`/`dirname function absolute_path { echo "$PWD/"; }
alias ap="absolute_path"
`
$0
stores the exact string how the script was executed.
$0
存储脚本如何执行的确切字符串。
For e.g. if the script was launched like this: $> ../../test/test.csh
,
$script_dir
will contain /home/abc/sandbox/v1/../../test
例如,如果脚本是这样启动的:$> ../../test/test.csh
,
$script_dir
将包含/home/abc/sandbox/v1/../../test
回答by Roman Rhrn Nesterov
You can save this in your shell.rc
or just put in console
您可以将其保存在您的shell.rc
或只是放在控制台中
ap somefile.txt
example:
例子:
/home/user/somefile.txt
will output
会输出
find $PWD -type f | grep "filename"
回答by Anshul Gupta
find $PWD -type f -name "*filename*"
or
或者
find / -name file.txt
回答by user2618464
This will give you absolute path of the file:
这将为您提供文件的绝对路径:
% 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
回答by Adam Matan
You could use the fpn (full path name)script:
您可以使用fpn(完整路径名)脚本:
find / -samefile file.txt -print
fpn
is not a standard Linux package, but it's a free and open github projectand you could set it up in a minute.
fpn
不是一个标准的 Linux 包,但它是一个免费和开放的 github 项目,你可以在一分钟内设置它。
回答by wildplasser
find /bin -samefile /bin/gunzip -ls
Will find all the links to the file with the same inode numberas file.txt
将找到与file.txt具有相同inode 编号的文件的所有链接
adding a -xdev
flag will avoid find
to cross device boundaries ("mount points"). (But this will probably cause nothing to be found if the find
does not start at a directory on the same device as file.txt
)
添加-xdev
标志将避免find
跨越设备边界(“挂载点”)。(但是,如果find
不在与 相同设备上的目录中启动,则这可能不会导致找不到任何内容file.txt
)
Do note that find
can report multiple paths for a single filesystem object, because an Inode can be linked by more than one directory entry, possibly even using different names. For instance:
请注意,find
可以为单个文件系统对象报告多个路径,因为一个 Inode 可以由多个目录条目链接,甚至可能使用不同的名称。例如:
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
Will output:
将输出:
##代码##