Create a symbolic link do navigate with cd

Solution 1:

The problem with creating a symbolic link in that manner is that you are limited to where the symbolic link is created. It also adds to the problem if you have a directory ../d/.. somewhere on your system.

There are a couple of ways to solve this....

Create an Alias

In terminal, you can create an alias by issuing the command

alias cdd='cd ~/Documents'

The benefit here is that it will work anywhere without having to add it to your PATH.

To make it permanent, add the command to your .bash_profile in your home directory.

Make a variable cd-able

Also, in your .bash_profile add the following

shopt -s cdable_vars 
export Docs=$HOME/Documents

Now, when you type cd Docs it will expand out HOME to whatever your user directory is plus the directory you specified. (/Users/yourusername/Documents)

Personally, I did this with a variable called icloud as follows:

export  icloud=$HOME/Library/Mobile\ Documents/com~apple~CloudDocs/

Now, I can reference my iCloud documents location without having to remember that lengthy path.

Solution 2:

You can define variables pointing to your directories. For example:

[~]$ export d=~/Documents

[~]$ ls -l $d
[~]$ cd $d
[~/Documents]$ 

In bash you can use cdable_vars option to be able to cd without preceding $:

[~]$ shopt -s cdable_vars
[~]$ export d=~/Documents

[~]$ cd d
[~/Documents]$ 

But it does not allow dropping $ for other commands, so ls -l d won't work.