0

I created a symlink in my home folder to another directory:

$ pwd
/home/user1
$ ln -s /usr/local/dir/ shortcut
$ ls -l
................................... shortcut -> /usr/local/dir/
$ cd shortcut
$ pwd
/home/user1/shortcut

I do end up in that dir so I can see all the files in there but the pwd is confusing and could cause issues. Is there a better way to this so that it acts a shortcut and actually shows /usr/local/dir when I go into cd shortcut instead of showing the new path?

  • Cross-site duplicate of pwd without symlinks and Strange difference between pwd and /bin/pwd. – G-Man Jan 11 '17 at 1:28
  • 1
    I'm voting to close this question as off-topic because it has already been answered on Unix & Linux Stack Exchange. – G-Man Jan 11 '17 at 1:30
  • @G-Man, is this a case of the question being answered elsewhere on a similar question, or the OP cross-posting the same question and getting an answer on another site? The links in the first comment are to older questions. The link in the close comment is to the site rather than a specific post and the OP isn't a user there. Just the existence of an answer on another site wouldn't make it off-topic here. – fixer1234 Jan 11 '17 at 23:02
1

Both cd and pwd have flags to use the physical directory structure.

cd

$ cd --help
...
  -P   use the physical directory structure without following
       symbolic links: resolve symbolic links in DIR before
       processing instances of `..'
...

e.g

$ cd /var/tmp 
$ ln -s /home/user test 
$ cd -P test
$ pwd
/home/user

Above, we use cd -P to use the physical directory structure, so now pwd gives us the physical path.

pwd

$ pwd
...
  -P    print the physical directory, without any symbolic links
...

e.g

$ cd /var/tmp
$ ln -s /home/user test 
$ cd test
$ pwd
/var/tmp/test
$ pwd -P
/home/user

Above, we cd'd into the directory as normal, so pwd gives the symbolic path. Using pwd -P will give the physical path.

Set globally in bash

If you are using bash you can tell it to ignore symbolic links entirely and just use the physical directory structure by running set -P (shorthand for set -o physical, see the `bash man page). This is likely possible with other shells too.

  • This resets to default on every logout. So while logged in, running the command gives the desired results but logging out and back resets it to default. Is there a way to make it remember the "set -P"? – prl77 Jan 24 '17 at 22:54
  • 1
    Ah bad wording on my part, that only persists per shell. You can set this in ~/.bashrc, this will be loaded for every interactive shell, then to make sure it's loaded for ssh etc... you can place it in ~/.bash_profile or run source ~/.bashrc from ~/.bash_profile - it may already be sourced, depending on the distro. – wayofthepie Jan 24 '17 at 23:19
0

Use $CDPATH

$ CDPATH=/usr/local
$ cd dir
$ pwd
/usr/local/dir
  • ISTM that that is more of a work-around than a real answer, and might be better as a comment. – G-Man Jan 11 '17 at 1:39
  • I agree. The answer from wayofthepie above is spot on and what I was hoping for. Easy, global, built-in. Not a negative aspect to it. – prl77 Jan 12 '17 at 18:55

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.