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?

prl77
  • 272

2 Answers2

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.

0

Use $CDPATH

$ CDPATH=/usr/local
$ cd dir
$ pwd
/usr/local/dir
Ipor Sircer
  • 4,194