bash/fish command to print absolute path to a file
Question: is there a simple sh/bash/zsh/fish/... command to print the absolute path of whichever file I feed it?
Usage case: I'm in directory /a/b
and I'd like to print the full path to file c
on the command-line so that I can easily paste it into another program: /a/b/c
. Simple, yet a little program to do this could probably save me 5 or so seconds when it comes to handling long paths, which in the end adds up. So it surprises me that I can't find a standard utility to do this — is there really none?
Here's a sample implementation, 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)
尝试realpath
。
~ $ realpath example.txt
/home/username/example.txt
尝试使用readlink
来解析符号链接:
readlink -e /foo/bar/baz
#! /bin/sh
echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
链接地址: http://www.djcxy.com/p/56858.html
上一篇: 自动捕获上次执行的命令输出到一个变量