cd to a symlink, is the same to cd to original folder?

7,834

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

Share:
7,834

Related videos on Youtube

IAmJulianAcosta
Author by

IAmJulianAcosta

I love to create new things. Developer because I'm good at it. Love working while traveling

Updated on September 18, 2022

Comments

  • IAmJulianAcosta
    IAmJulianAcosta over 1 year

    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?

  • user4556274
    user4556274 over 7 years
    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).
  • Wil
    Wil over 5 years
    cd -P .. will move take you to the Physical parent directory instead of popping one branch off the logical directory tree.