Suppose I'm in /path/to/dir. Within this dir is another dir called subdir.

Is there a command I can issue which outputs the full path to subdir, no matter how it is identified? For example:

$ cmd subdir

$ cmd /path/to/dir/subdir

2 Answers 2


coreutils' realpath does the trick:

realpath subdir

and it works however the directory (or file) is specified:

realpath /blah/blah2/subdir
realpath ../blah2/subdir
function somepath () {
  [ -z "$1" ] && { pwd; return; }
  (cd -P -- "$1" && pwd)

Simply creates a subshell (so that the cd doesn't affect your current shell) and prints the cwd. (Edited in a test for the "no parameter" case)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .