Fix for ENOTDIR in realpath calls by stripping off the filename

See: http://man7.org/linux/man-pages/man3/realpath.3.html