9

I use the realpath command, but it always adds the original input at the end of the path:

$ pwd
/homes/yosefkl
$ realpath yosefkl
/homes/yosefkl/yosefkl
  • You're asking for a directory inside your current working directory. Try this instead: realpath . – penguin359 Dec 28 '17 at 7:54
17

You gave realpath a relative path, to a file in the current directory.

The file (which may be a directory) may or may not exist. realpath does not care by default. It reports the path regardless of whether its last element exists.

If you want it to only report paths that exist, use the -e flag:

zanna@toaster:~/playground$ realpath playground
/home/zanna/playground/playground
zanna@toaster:~/playground$ realpath ~/playground
/home/zanna/playground
zanna@toaster:~/playground$ realpath -e playground
realpath: playground: No such file or directory
11

That's because you're giving it a path that doesn't exist, so it simply prints out the current directory and whatever you gave it, assuming that would be the path. Note that the man page specifies that:

Print the resolved absolute file name; all but the last component must exist

So it allows execution with a nonexistent target. To illustrate, consider these examples:

$ pwd
/home
$ ls
lost+found  terdon
$ realpath terdon
/home/terdon
$ realpath nonExistentDir
/home/nonExistentDir

Or, to duplicate what you did:

$ pwd
/home/terdon
$ realpath terdon
/home/terdon/terdon
5

No, it only tells you that the path to the (probably non-existent) file yosefkl located in the current directory (/homes/yosefkl) is /homes/yosefkl/yosefkl. Try

realpath "$PWD"

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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