热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何使用readlink获取文件的完整路径?

本文介绍了使用readlink命令获取文件的完整路径的简单方法,并提供了一个示例命令来打印文件的完整路径。共有28种解决方案可供选择。

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

28 个解决方案

#1


786  

Use readlink:

使用指向:

readlink -f file.txt

#2


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

#3


59  

The following usually does the trick:

下面的技巧通常是这样的:

 echo $(cd $(dirname "$1") && pwd -P)/$(basename "$1")

#4


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

#5


12  

find $PWD -type f | grep "filename"

or

find $PWD -type f -name "*filename*"

#6


4  

If you are in the same directory as the file:

如果您位于与文件相同的目录中:

ls "`pwd`/file.txt"

Replace file.txt with your target filename.

替换文件。使用目标文件名txt。

#7


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项目,您可以在一分钟内设置它。

#8


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。

#9


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用户的框中工作。

#10


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.

这将复制文件的完整路径到剪贴板。

In Linux you can use :-

realpath yourfile to get the full path of a file as suggested by many.

realpath您的文件,以获得一个文件的完整路径,正如许多人建议的那样。

#11


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

#12


0  

echo $(cd $(dirname "$1") && pwd -P)/$(basename "$1")

This is explanation of what is going on at @ZeRemz's answer:

这解释了@ZeRemz的答案:

  1. This script get relative path as argument "$1"
  2. 这个脚本获得相对路径作为参数“$1”
  3. Then we get dirname part of that path (you can pass either dir or file to this script): dirname "$1"
  4. 然后,我们将得到该路径的dirname部分(您可以将dir或文件传递给该脚本):dirname“$1”
  5. Then we cd "$(dirname "$1") into this relative dir
  6. 然后我们cd“$(dirname“$1”)进入这个相对目录。
  7. && pwd -P and get absolute path for it. -P option will avoid all symlinks
  8. 和pwd -P,并获得绝对路径。-P选项将避免所有符号链接。
  9. After that we append basename to absolute path: $(basename "$1")
  10. 之后,我们将basename添加到绝对路径:$(basename“$1”)
  11. As final step we echo it
  12. 作为最后一步,我们要回应它。

#13


0  

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/。

#14


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列表或任何类似的数据结构中。

#15


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} \'
}

#16


-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

#17


-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)

(我从另一个堆栈溢出的答案中复制了一些代码,但再也找不到那个答案了)

#18


-1  

the easiest way I found is

我找到的最简单的方法是。

for i in `ls`; do echo "`pwd`/$i"; done

it works well for me

这对我来说很有效。

#19


-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")"; }
}

#20


-1  

Usually:

通常:

find `pwd` | grep 

Alternatively, just for the current folder:

或者,只针对当前文件夹:

find `pwd` -maxdepth 1 | grep 

#21


-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

戴夫

#22


-1  

In Mac OSX, do the following steps:

在Mac OSX中,执行以下步骤:

  1. cd into the directory of the target file.
  2. cd进入目标文件的目录。
  3. Type either of the following terminal commands.
  4. 输入以下终端命令。
Terminal
ls "`pwd`/file.txt"
echo $(pwd)/file.txt
  1. Replace file.txt with your actual file name.
  2. 替换文件。使用您的实际文件名。
  3. Press Enter
  4. 按回车键

#23


-1  

This works with both Linux and Mac OSX ..

这与Linux和Mac OSX兼容。

 echo $(pwd)$/$(ls file.txt)

#24


-1  

Another Linux utility, that does this job:

另一个Linux实用程序,做这个工作:

fname 

#25


-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中获取文件的路径,控制点击文件,向下滚动到底部的“服务”。您可以得到许多选择,包括“复制路径”和“复制完整路径”。点击其中一个,就可以在剪贴板上找到路径。

#26


-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命令是正确的。

#27


-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

#28


-9  

This will give you absolute path of the file:

这将给出文件的绝对路径:

find / -name file.txt 

推荐阅读
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    本文介绍了关于人工智能、神经网络和深度学习的知识点,并提供了YOLOv7基于自己的数据集从零构建模型完整训练、推理计算的详细教程。文章还提到了郑州最低生活保障的话题。对于从事目标检测任务的人来说,YOLO是一个熟悉的模型。文章还提到了yolov4和yolov6的相关内容,以及选择模型的优化思路。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
author-avatar
Karson2012
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有