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
?
Ask Different is a question and answer site for power users of Apple hardware and software. It only takes a minute to sign up.
Sign up to join this communityIs 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
?
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....
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.
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.
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.