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

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.