热门标签 | 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 

推荐阅读
  • 360SRC安全应急响应:从漏洞提交到修复的全过程
    本文详细介绍了360SRC平台处理一起关键安全事件的过程,涵盖从漏洞提交、验证、排查到最终修复的各个环节。通过这一案例,展示了360在安全应急响应方面的专业能力和严谨态度。 ... [详细]
  • 本文探讨了如何在 F# Interactive (FSI) 中通过 AddPrinter 和 AddPrintTransformer 方法自定义类型(尤其是集合类型)的输出格式,提供了详细的指南和示例代码。 ... [详细]
  • 并发编程 12—— 任务取消与关闭 之 shutdownNow 的局限性
    Java并发编程实践目录并发编程01——ThreadLocal并发编程02——ConcurrentHashMap并发编程03——阻塞队列和生产者-消费者模式并发编程04——闭锁Co ... [详细]
  • 探讨ChatGPT在法律和版权方面的潜在风险及影响,分析其作为内容创造工具的合法性和合规性。 ... [详细]
  • 本文将详细探讨 Java 中提供的不可变集合(如 `Collections.unmodifiableXXX`)和同步集合(如 `Collections.synchronizedXXX`)的实现原理及使用方法,帮助开发者更好地理解和应用这些工具。 ... [详细]
  • 在维护公司项目时,发现按下手机的某个物理按键后会激活相应的服务,并在屏幕上模拟点击特定坐标点。本文详细介绍了如何使用ADB Shell Input命令来模拟各种输入事件,包括滑动、按键和点击等。 ... [详细]
  • dotnet 通过 Elmish.WPF 使用 F# 编写 WPF 应用
    本文来安利大家一个有趣而且强大的库,通过F#和C#混合编程编写WPF应用,可以在WPF中使用到F#强大的数据处理能力在GitHub上完全开源Elmis ... [详细]
  • 自己用过的一些比较有用的css3新属性【HTML】
    web前端|html教程自己用过的一些比较用的css3新属性web前端-html教程css3刚推出不久,虽然大多数的css3属性在很多流行的浏览器中不支持,但我个人觉得还是要尽量开 ... [详细]
  • 利用决策树预测NBA比赛胜负的Python数据挖掘实践
    本文通过使用2013-14赛季NBA赛程与结果数据集以及2013年NBA排名数据,结合《Python数据挖掘入门与实践》一书中的方法,展示如何应用决策树算法进行比赛胜负预测。我们将详细讲解数据预处理、特征工程及模型评估等关键步骤。 ... [详细]
  • 中科院学位论文排版指南
    随着毕业季的到来,许多即将毕业的学生开始撰写学位论文。本文介绍了使用LaTeX排版学位论文的方法,特别是针对中国科学院大学研究生学位论文撰写规范指导意见的最新要求。LaTeX以其精确的控制和美观的排版效果成为许多学者的首选。 ... [详细]
  • 本文深入探讨了SQL数据库中常见的面试问题,包括如何获取自增字段的当前值、防止SQL注入的方法、游标的作用与使用、索引的形式及其优缺点,以及事务和存储过程的概念。通过详细的解答和示例,帮助读者更好地理解和应对这些技术问题。 ... [详细]
  • 本文介绍如何使用MFC和ADO技术调用SQL Server中的存储过程,以查询指定小区在特定时间段内的通话统计数据。通过用户界面选择小区ID、开始时间和结束时间,系统将计算并展示小时级的通话量、拥塞率及半速率通话比例。 ... [详细]
  • 主板IO用W83627THG,用VC如何取得CPU温度,系统温度,CPU风扇转速,VBat的电压. ... [详细]
  • 历经三十年的开发,Mathematica 已成为技术计算领域的标杆,为全球的技术创新者、教育工作者、学生及其他用户提供了一个领先的计算平台。最新版本 Mathematica 12.3.1 增加了多项核心语言、数学计算、可视化和图形处理的新功能。 ... [详细]
  • 本文档介绍了如何在Visual Studio 2010环境下,利用C#语言连接SQL Server 2008数据库,并实现基本的数据操作,如增删改查等功能。通过构建一个面向对象的数据库工具类,简化了数据库操作流程。 ... [详细]
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社区 版权所有