1

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?

3

2 Answers 2

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.

2
  • 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, 2017 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. Jan 24, 2017 at 23:19
0

Use $CDPATH

$ CDPATH=/usr/local
$ cd dir
$ pwd
/usr/local/dir
2
  • ISTM that that is more of a work-around than a real answer, and might be better as a comment. Jan 11, 2017 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, 2017 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .