4

I want to create a symbolic link just for convenience (I don't want a type a long path), so if I do something like:

ln -s /foo/bar/baz ~/baz
cd baz

All commands that I run while I'm in ~/baz will run exactly the same way if I am in /foo/bar/baz?

muru
  • 197,895
  • 55
  • 485
  • 740

1 Answers1

2

Almost... The cd and pwd commands will behave as if you are in ~/baz (although you can cd to subdirectories of /foo/bar/baz inside ~/baz, when you cd .. you will be in ~)

All other commands will behave as if you were in the real directory and all permissions will be preserved (of course - that's why we say symlinks have "dummy permissions").

This includes (potentially confusingly) commands with relative paths that extend outside the directory. With the exception of cd, which considers you to be in ~/baz, you must make sure you use them as if you are in the real directory, not the symlink. For example if you wanted to ls the contents of /foo/bar, in ~/baz you could do ls .. and if you wanted to symlink a file in foo/bar in /foo/bar/baz(let's call it kitten) then inside ~/baz you could do ln -s ../kitten kitten

Zanna
  • 70,465
  • 3
    pwd -L will give your logical working directory (respecting symlinks). pwd -P will give your physical working directory (ignoring symlinks). To keep things confusing, the bash builtin pwd defaults to pwd -L while /bin/pwd on an ubuntu system defaults to pwd -P. So it is true that pwd will behave as if you are in ~/baz as long as you are using the bash builtin. (Not sure about other shells). – user4556274 Aug 27 '16 at 00:32
  • 7
    cd -P .. will move take you to the Physical parent directory instead of popping one branch off the logical directory tree. – Wil Dec 14 '18 at 17:24