Is there an easy way I can print the full path of file.txt
?
有没有一种简单的方法可以打印完整的文件路径。三种吗?
file.txt = /nfs/an/disks/jj/home/dir/file.txt
The
<命令>
dir> file.txt
should print
应该打印
/nfs/an/disks/jj/home/dir/file.txt
786
Use readlink:
使用指向:
readlink -f file.txt
115
I suppose you are using Linux.
我想你正在使用Linux。
I found a utility called realpath
in coreutils 8.15.
我在coreutils 8.15找到了一个叫做realpath的工具。
realpath realpath
/data/ail_data/transformed_binaries/coreutils/test_folder_realpath/realpath
59
The following usually does the trick:
下面的技巧通常是这样的:
echo $(cd $(dirname "$1") && pwd -P)/$(basename "$1")
21
I know there'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
12
find $PWD -type f | grep "filename"
or
或
find $PWD -type f -name "*filename*"
4
If you are in the same directory as the file:
如果您位于与文件相同的目录中:
ls "`pwd`/file.txt"
Replace file.txt
with your target filename.
替换文件。使用目标文件名txt。
2
You could use the fpn (full path name) script:
您可以使用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
is not a standard Linux package, but it's a free and open github project and you could set it up in a minute.
fpn不是标准的Linux包,但它是一个免费的、开放的github项目,您可以在一分钟内设置它。
1
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 $0`
$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
例如,如果脚本是这样启动的:$> ../. /测试/测试。csh, $script_dir将包含/home/ abc/sandbox/v1// test。
1
For Mac OS X, I replaced the utilities that come with the operating system and replaced them with a newer version of coreutils. This allows you to access tools like readlink -f
(for absolute path to files) and realpath
(absolute path to directories) on your Mac.
对于Mac OS X,我替换了操作系统附带的实用程序,并将它们替换为新版本的coreutils。这允许您访问Mac上的readlink -f(对于文件的绝对路径)和realpath(绝对路径到目录)。
The Homebrew version appends a 'G' (for GNU Tools) in front of the command name -- so the equivalents become greadlink -f FILE
and grealpath DIRECTORY
.
Homebrew版本在命令名前面附加了一个“G”(用于GNU工具),所以它等价于greadlink -f文件和grealpath目录。
Instructions for how to install the coreutils/GNU Tools on Mac OS X through Homebrew can be found in this StackExchange arcticle.
关于如何在Mac OS X上安装coreutils/GNU工具的说明可以在这个StackExchange的arcticle中找到。
NB: The readlink -f
and realpath
commands should work out of the box for non-Mac Unix users.
NB: readlink -f和realpath命令应该在非mac Unix用户的框中工作。
1
In windows you can
在windows中你可以
Hold shift and right click on a file which gives you can option called "Copy as Path"
This will copy the full path of the file to clipboard.
这将复制文件的完整路径到剪贴板。
realpath yourfile
to get the full path of a file as suggested by many.
realpath您的文件,以获得一个文件的完整路径,正如许多人建议的那样。
0
You can save this in your "shell.rc" or just put in console
您可以将其保存在您的“shell”中。或者直接放到控制台。
function absolute_path { echo "$PWD/$1"; }
函数absolute_path {echo "$PWD/$1";}
alias ap="absolute_path"
别名美联社= " absolute_path "
example:
例子:
ap somefile.txt
美联社somefile.txt
will output
将输出
/home/user/somefile.txt
/home/user/somefile.txt
0
echo $(cd $(dirname "$1") && pwd -P)/$(basename "$1")
This is explanation of what is going on at @ZeRemz's answer:
这解释了@ZeRemz的答案:
"$1"
dirname "$1"
cd "$(dirname "$1")
into this relative dir && pwd -P
and get absolute path for it. -P
option will avoid all symlinks$(basename "$1")
echo
it0
I know that this is an old question now, but just to add to the information here:
我知道这是一个老问题,但我想补充一点:
The Linux command which
can be used to find the filepath of a command file, i.e.
可以用来查找命令文件的filepath的Linux命令,即
$ which ls
/bin/ls
There are some caveats to this; please see https://www.cyberciti.biz/faq/how-do-i-find-the-path-to-a-command-file/.
这里有一些警告;请参阅https://www.cyberciti.biz/faq/how-do-i-find-the-path-to-a-command-file/。
0
Works on Mac, Linux, *nix:
在Mac, Linux, *nix上工作:
This will give you a quoted csv of all files in the current dir:
这将为您提供当前目录中所有文件的引用csv:
ls | xargs -I {} echo "$(pwd -P)/{}" | xargs | sed 's/ /","/g'
The output of this can be easily copied into a python list or any similar data structure.
它的输出可以很容易地复制到python列表或任何类似的数据结构中。
0
This worked pretty well for me. It doesn't rely on the file system (a pro/con depending on need) so it'll be fast; and, it should be portable to most any *NIX. It does assume the passed string is indeed relative to the PWD and not some other directory.
这对我来说很有效。它不依赖于文件系统(根据需要,一个pro/con),所以它会很快;而且,它应该可以移植到任何一个*NIX。它假设传递的字符串确实是相对于PWD而不是其他目录。
function abspath () {
echo $1 | awk '\
# Root parent directory refs to the PWD for replacement below
/^\.\.\// { sub("^", "./") } \
# Replace the symbolic PWD refs with the absolute PWD \
/^\.\// { sub("^\.", ENVIRON["PWD"])} \
# Print absolute paths \
/^\// {print} \'
}
-1
find / -samefile file.txt -print
Will find all the links to the file with the same inode number as file.txt
将找到文件的所有链接与文件.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在与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可以通过多个目录条目链接,甚至可能使用不同的名称。例如:
find /bin -samefile /bin/gunzip -ls
Will output:
将输出:
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
-1
fp () {
PHYS_DIR=`pwd -P`
RESULT=$PHYS_DIR/$1
echo $RESULT | pbcopy
echo $RESULT
}
Copies the text to your clipboard and displays the text on the terminal window.
将文本复制到剪贴板,并在终端窗口上显示文本。
:)
:)
(I copied some of the code from another stack overflow answer but cannot find that answer anymore)
(我从另一个堆栈溢出的答案中复制了一些代码,但再也找不到那个答案了)
-1
the easiest way I found is
我找到的最简单的方法是。
for i in `ls`; do echo "`pwd`/$i"; done
it works well for me
这对我来说很有效。
-1
This will work for both file and folder:
这将适用于文件和文件夹:
getAbsolutePath(){
[[ -d $1 ]] && { cd "$1"; echo "$(pwd -P)"; } ||
{ cd "$(dirname "$1")" || exit 1; echo "$(pwd -P)/$(basename "$1")"; }
}
-1
Usually:
通常:
find `pwd` | grep
Alternatively, just for the current folder:
或者,只针对当前文件夹:
find `pwd` -maxdepth 1 | grep
-1
I like many of the answers already given, but I have found this really useful, especially within a script to get the full path of a file, including following symlinks and relative references such as .
and ..
我喜欢许多已经给出的答案,但我发现这非常有用,尤其是在一个脚本中获取文件的完整路径,包括下面的符号链接和相关引用。和. .
dirname `readlink -e relative/path/to/file`
Which will return the full path of the file
from the root path onwards. This can be used in a script so that the script knows which path it is running from, which is useful in a repository clone which could be located anywhere on a machine.
这将从根路径开始返回文件的完整路径。这可以在脚本中使用,这样脚本就知道它从哪条路径运行,这在存储库的克隆中很有用,它可以位于机器的任何位置。
basePath=`dirname \`readlink -e $0\``
I can then use the ${basePath}
variable in my scripts to directly reference other scripts.
然后,我可以在脚本中使用${basePath}变量来直接引用其他脚本。
Hope this helps,
希望这有助于
Dave
戴夫
-1
In Mac OSX, do the following steps:
在Mac OSX中,执行以下步骤:
cd
into the directory of the target file.ls "`pwd`/file.txt"
echo $(pwd)/file.txt
file.txt
with your actual file name.-1
This works with both Linux and Mac OSX ..
这与Linux和Mac OSX兼容。
echo $(pwd)$/$(ls file.txt)
-1
Another Linux utility, that does this job:
另一个Linux实用程序,做这个工作:
fname
-1
For Mac OS, if you just want to get the path of a file in the finder, control click the file, and scroll down to "Services" at the bottom. You get many choices, including "copy path" and "copy full path". Clicking on one of these puts the path on the clipboard.
对于Mac OS,如果您只是想要在finder中获取文件的路径,控制点击文件,向下滚动到底部的“服务”。您可以得到许多选择,包括“复制路径”和“复制完整路径”。点击其中一个,就可以在剪贴板上找到路径。
-3
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 $
This also give the full path and file name at console
这也提供了控制台的完整路径和文件名。
Off-topic: This method just gives relative links, not absolute. The readlink -f
command is the right one.
Off-topic:这个方法只提供相对链接,而不是绝对链接。readlink -f命令是正确的。
-3
Create a function like the below (echoes the absolute path of a file with pwd and adds the file at the end of the path:
创建如下的函数(与pwd的文件的绝对路径相呼应,并在路径的末尾添加文件:
abspath() { echo $(pwd "$1")/"$1"; }
Now you can just find any file path:
现在您可以找到任何文件路径:
abspath myfile.ext
-9
This will give you absolute path of the file:
这将给出文件的绝对路径:
find / -name file.txt