3

Is it possible to create a symbolic link to easier change directories? like

ln -s /Users/mles/Documents /usr/local/bin/d

so I can cd d instead of cd ~/Documents?

mles
  • 206
  • Here is a generic answer to this question: https://apple.stackexchange.com/a/97312/22003 . – dan Feb 09 '18 at 17:07

2 Answers2

3

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.

Allan
  • 101,432
1

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.

techraf
  • 3,958