问:有没有简单的sh/bash/zsh/fish/…命令打印任何文件的绝对路径我馈送它?

用例:我在目录/a/b中,我想在命令行上打印文件c的完整路径,这样我就可以轻松地将它粘贴到另一个程序:/a/b/c中。这很简单,但是在处理长路径时,一个小程序可能会为我节省5秒左右的时间。因此,让我感到惊讶的是,我找不到一个标准的实用程序来做这件事——真的没有吗?

下面是一个示例实现,abspath.py:

#!/usr/bin/python
# Author: Diggory Hardy <diggory.hardy@gmail.com>
# Licence: public domain
# Purpose: print the absolute path of all input paths

import sys
import os.path
if len(sys.argv)>1:
    for i in range(1,len(sys.argv)):
        print os.path.abspath( sys.argv[i] )
    sys.exit(0)
else:
    print >> sys.stderr, "Usage: ",sys.argv[0]," PATH."
    sys.exit(1)

当前回答

如果你没有readlink或realpath实用工具,那么你可以使用下面的函数,它可以在bash和zsh中工作(不确定其他的)。

abspath () { case "$1" in /*)printf "%s\n" "$1";; *)printf "%s\n" "$PWD/$1";; esac; }

这也适用于不存在的文件(就像python函数os.path.abspath)。

不幸的是,沐浴。/somefile不能去掉圆点。

其他回答

我用单线

(cd ${FILENAME%/*}; pwd)

但是,这只能在$FILENAME有一个实际存在的任何类型的前导路径(相对或绝对)时使用。如果根本没有前导路径,那么答案就是$PWD。如果前导路径不存在,则答案可能是不确定的,否则,如果路径是绝对路径,则答案只是${FILENAME%/*}。

把这些放在一起,我建议使用下面的函数

function abspath() {
  # argument 1: file pathname (relative or absolute)
  # returns: file pathname (absolute)
  if [ "$1" == "${1##*/}" ]; then # no path at all
    echo "$PWD"
  elif [ "${1:0:1}" == "/" -a "${1/../}" == "$1" ]; then # strictly absolute path
    echo "${1%/*}"
  else # relative path (may fail if a needed folder is non-existent)
    echo "$(cd ${1%/*}; pwd)"
  fi
}

还要注意,这只适用于bash和兼容的shell。我不相信替换在简单的shell sh中起作用。

使用realpath

$ realpath example.txt
/home/username/example.txt

在Ruby中获取绝对路径的另一种方法:

realpath() {ruby -e "require 'Pathname';将Pathname.new(1美元).realpath.to_s”;}

不带参数(当前文件夹)和相对和绝对文件或文件夹路径作为参数。

忘记readlink和realpath,它们可能安装也可能不安装在您的系统上。

在上面扩展dogbane的答案,它被表示为一个函数:

#!/bin/bash
get_abs_filename() {
  # $1 : relative filename
  echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
}

然后你可以这样使用它:

myabsfile=$(get_abs_filename "../../foo/bar/file.txt")

它是如何工作的,为什么工作?

该解决方案利用了这样一个事实,即Bash内置的pwd命令在调用时不带参数地打印当前目录的绝对路径。

为什么我喜欢这个解?

它是可移植的,并且不需要readlink或realpath,这在给定的Linux/Unix发行版的默认安装中通常不存在。

如果dir不存在呢?

如上所述,如果给定的目录路径不存在,该函数将失败并在stderr上打印。这可能不是你想要的。你可以展开函数来处理这种情况:

#!/bin/bash
get_abs_filename() {
  # $1 : relative filename
  if [ -d "$(dirname "$1")" ]; then
    echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
  fi
}

现在如果父dirs不存在,它将返回一个空字符串。

你如何处理拖尾?'或'。输入?

在这种情况下,它确实给出了一条绝对路径,但不是最小路径。它看起来像:

/Users/bob/Documents/..

如果您想解决'..你需要把脚本写成这样:

get_abs_filename() {
  # $1 : relative filename
  filename=$1
  parentdir=$(dirname "${filename}")

  if [ -d "${filename}" ]; then
      echo "$(cd "${filename}" && pwd)"
  elif [ -d "${parentdir}" ]; then
    echo "$(cd "${parentdir}" && pwd)/$(basename "${filename}")"
  fi
}

在某些情况下,这个问题最上面的答案可能具有误导性。假设你要查找的文件的绝对路径在$ path变量中:

# node is in $PATH variable
type -P node
# /home/user/.asdf/shims/node
cd /tmp
touch node  # But because there is a file with the same name inside the current dir check out what happens below
readlink -e node
# /tmp/node
readlink -m node
# /tmp/node
readlink -f node
# /tmp/node
echo "$(cd "$(dirname "node")"; pwd -P)/$(basename "node")"
# /tmp/node
realpath node
# /tmp/node
realpath -e node
# /tmp/node

# Now let's say that for some reason node does not exist in current directory
rm node
readlink -e node
# <nothing printed>
readlink -m node    
# /tmp/node         # Note: /tmp/node does not exist, but is printed
readlink -f node
# /tmp/node         # Note: /tmp/node does not exist, but is printed
echo "$(cd "$(dirname "node")"; pwd -P)/$(basename "node")"
# /tmp/node         # Note: /tmp/node does not exist, but is printed
realpath node
# /tmp/node         # Note: /tmp/node does not exist, but is printed
realpath -e node
# realpath: node: No such file or directory

基于以上,我可以得出结论:realpath -e和readlink -e可以用于查找文件的绝对路径,我们期望存在于当前目录中,而不受$ path变量的影响。唯一的区别是realpath输出到stderr,但如果没有找到file,两者都会返回错误代码:

cd /tmp
rm node
realpath -e node ; echo $?
# realpath: node: No such file or directory
# 1
readlink -e node ; echo $?
# 1

现在,如果您想要一个存在于$ path中的文件的绝对路径a,那么下面的命令将是合适的,这与当前目录中是否存在同名文件无关。

type -P example.txt
# /path/to/example.txt

# Or if you want to follow links
readlink -e $(type -P example.txt)
# /originalpath/to/example.txt

# If the file you are looking for is an executable (and wrap again through `readlink -e` for following links )
which executablefile
# /opt/bin/executablefile

如果没有,回退到$PATH,示例:

cd /tmp
touch node
echo $(readlink -e node || type -P node)
# /tmp/node
rm node
echo $(readlink -e node || type -P node)
# /home/user/.asdf/shims/node