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)
I have placed the following script on my system & I call it as a bash alias for when I want to quickly grab the full path to a file in the current dir:
I am not sure why, but, on OS X when called by a script "$PWD" expands to the absolute path. When the find command is called on the command line, it doesn't. But it does what I want... enjoy.
Forget about
readlink
andrealpath
which may or may not be installed on your system.Expanding on dogbane's answer above here it is expressed as a function:
you can then use it like this:
How and why does it work?
The solution exploits the fact that the Bash built-in
pwd
command will print the absolute path of the current directory when invoked without arguments.Why do I like this solution ?
It is portable and doesn't require neither
readlink
orrealpath
which often does not exist on a default install of a given Linux/Unix distro.What if dir doesn't exist?
As given above the function will fail and print on stderr if the directory path given does not exist. This may not be what you want. You can expand the function to handle that situation:
Now it will return an empty string if one the parent dirs do not exist.
How do you handle trailing '..' or '.' in input ?
Well, it does give an absolute path in that case, but not a minimal one. It will look like:
If you want to resolve the '..' you will need to make the script like:
If you don't have readlink or realpath utilities than you can use following function which works in bash and zsh (not sure about the rest).
This also works for nonexistent files (as does the python function
os.path.abspath
).Unfortunately
abspath ./../somefile
doesn't get rid of the dots.Here's a zsh-only function that I like for its compactness. It uses the ‘A’ expansion modifier — see zshexpn(1).
Hey guys I know it's an old thread but I am just posting this for reference to anybody else who visited this like me. If i understood the question correctly, I think the
locate $filename
command. It displays the absolute path of the file supplied, but only if it exists.There is generally no such thing as the
absolute path
to a file (this statement means that there may be more than one in general, hence the use of the definite article the is not appropriate). Anabsolute path
is any path that start from the root "/" and designates a file without ambiguity independently of the working directory.(see for example wikipedia).A
relative path
is a path that is to be interpreted starting from another directory. It may be the working directory if it is arelative path
being manipulated by an application (though not necessarily). When it is in a symbolic link in a directory, it is generally intended to be relative to that directory (though the user may have other uses in mind).Hence an absolute path is just a path relative to the root directory.
A path (absolute or relative) may or may not contain symbolic links. If it does not, it is also somewhat impervious to changes in the linking structure, but this is not necessarily required or even desirable. Some people call
canonical path
( orcanonical file name
orresolved path
) anabsolute path
in which all symbolic links have been resolved, i.e. have been replaced by a path to whetever they link to. The commandsrealpath
andreadlink
both look for a canonical path, but onlyrealpath
has an option for getting an absolute path without bothering to resolve symbolic links (along with several other options to get various kind of paths, absolute or relative to some directory).This calls for several remarks:
realpath
andreadlink
have options to account for that.canonical
. Hence the concept is time (or environment) dependent.canonical path
to a file, for two reasons:ro
) on several mount points.Hence, even with the much more restrictive definition of
canonical path
, there may be several canonical paths to a file. This also means that the qualifiercanonical
is somewhat inadequate since it usually implies a notion of uniqueness.This expands a brief discussion of the topic in an answer to another similar question at Bash: retrieve absolute path given relative
My conclusion is that
realpath
is better designed and much more flexible thanreadlink
. The only use ofreadlink
that is not covered byrealpath
is the call without option returning the value of a symbolic link.