1

I'd like to simplify a path containing parent directory (..) references, but without making it absolute.

E.g., a path like foo/bar/../baz should be simplified to foo/baz since the bar/.. part cancels out1.


1 Strictly speaking, this is not true if bar is a symlink somewhere else - but feel free to ignore this: I don't care if the simplification returns different results than the original path in the presence of symlinks.

BeeOnRope
  • 547

1 Answers1

3

I guess what you are looking for is realpath:

$ realpath --relative-to="$PWD" "foo/bar/../baz"
foo/baz

it also works fine with symlinks, use -s to ignore them:

-s, --strip, --no-symlinks don't expand symlinks

Ravexina
  • 2,638
  • 1
  • 19
  • 33