How to set up a shortcut to a directory in Mac OS X Terminal?

18

21

What I'd like to be able to do is when I'm in Terminal is to type:

cd myFolderShortcut

And for it to go straight to a directory I have a set up with this alias. Is this possible?

I did do a google for it and Aliases may be what I want, but I couldn't get it to work.

benhowdle89

Posted 2012-01-05T19:03:51.833

Reputation: 385

If what you want is a shorter command for cd'ing into a specific directory, all you need to do is add the following to your ~/.bashrc: alias myalias='cd /path/to/directory'. You can replace myalias with any custom command name you wish. – None – 2012-01-05T20:13:29.927

Check out https://github.com/joelthelion/autojump/wiki, it might also work for you.

– Daniel Beck – 2012-01-05T20:18:36.593

Answers

37

You don't need a shortcut file or anything like that.


You can set up an alias in ~/.bash_profile by adding the following line, so you just need to type myFolderShortcut to go there (without cd):

alias myFolderShortcut='cd /Users/danielbeck/Documents'

You also need to type the above command or

source ~/.bash_profile

to get this to take effect.  This works from any directory, but requires that your login shell be bash (which is OS X's default)


You can, of course, create symbolic links to other directories in your home directory. Then, when you open Terminal and are in your home directory, cding takes you to the linked directory.

ln -s /Users/danielbeck/Documents/Projects myProjectsDir

Then, type cd myProjectsDir and you're there (the displayed path contains myProjectsDir though, not Documents/Projects).

The symbolic link will show in Finder. To hide it, type chflags hidden myProjectsDir.

This will work only when you're in your home directory to start with (cd without arguments takes you there quickly; you can type both commands on the same line: cd && cd myProjectsDir).

Daniel Beck

Posted 2012-01-05T19:03:51.833

Reputation: 98 421

On El Capitan I get "No such file or directory" when typing in source ~/.bash_profile. – jkupczak – 2016-10-31T17:16:05.613

@jimmykup The paragraph above that command basically says you need to create the file first (or, well, to edit it, which implies creating if it doesn't exist). – Daniel Beck – 2016-10-31T17:47:13.963

Thanks. The issue was that my folder had a space in it. Wrapping apostrophes around shortcut='cd /Users/..' threw me off and I forgot to add " – jkupczak – 2016-10-31T19:58:02.580

It should be noted that once I close terminal I lose the shortcut functionality and have to redo it. Is this supposed to happen? – jkupczak – 2016-10-31T20:34:55.553

@jimmykup No, as .bash_profile is loaded in every newly opened bash shell (unless you're using iTerm, which IIRC makes bash load .bashrc instead). Did you follow the instructions in my answer? – Daniel Beck – 2016-11-01T10:08:39.033

@DanielBeck I did. I may just not understand Terminal well enough. Maybe there's something I'm suppose to do before your instructions? I open terminal, setup the alias, and then it's already working. source ~/.bash_profile always returns the error I mentioned above. And then closing Terminal and re-opening removes the alias. This is the extent of the steps I take. – jkupczak – 2016-11-01T20:19:58.410

@jimmykup My guess is the file you're editing has a different name, like bash_profile or .bash_profile.txt. ls to check and mv to fix. – Daniel Beck – 2016-11-01T20:26:47.693

Let us continue this discussion in chat.

– jkupczak – 2016-11-01T20:31:00.180

This is a great answer. Thanks. The wording/format is super confusing. e.g. when reading: "You can set up an alias in ~/.bash_profile by adding the following line, so you just need to type myFolderShortcut to go there (without cd):" You say add the following line but then switch your words to something else without a mention of the following line. I made an edit but it was rejected. Can you take a look into my suggestion and revise accordingly yourself?

– Honey – 2019-04-05T12:19:09.950

cd as cd ~ is my favorite alias. – Rob – 2012-01-05T20:29:07.720

@Rob It's not technically an alias. cd without argument just implies ~. I like cd - better, by the way. – Daniel Beck – 2012-01-05T20:36:42.150

Hi, I could not get your technique to work. I wanted to created a shortcut to my solr directory. It currently lives in my: alias solr='cd /Applications/MAMP/htdocs/website/WhoAt/solr/whoat/' Perhaps my path isn't correct? – Leon Gaban – 2013-06-10T19:41:03.140

@Leon Are you using the bash shell? – Daniel Beck – 2013-06-10T20:45:18.093

I think I'm using default I think... – Leon Gaban – 2013-06-10T22:35:33.287

My solution is as above, but I put the symbolic link in the root sudo ln -s /Applications/MAMP/htdocs www so that wherever I am I can go cd /www/... – Duncanmoo – 2013-08-08T07:48:08.380

4

I know this is old, but this might help someone.

After you follow @Daniel Beck's answer above, and add the alias to the bash_profile like he mentioned, you have to type the following in the terminal window:

source .bash_profile

This will make all your aliases work.

I've got this from this answer on stack overflow. It has worked for me. I hope it works for someone looking for this...

How Do I create a terminal shortcut to this path?

Salman Hasrat Khan

Posted 2012-01-05T19:03:51.833

Reputation: 141

2Thanks, this came in handy. Also source ~/.bash_profile might be a more general way of writing it – mattsven – 2015-09-15T01:43:02.507

1

Just one thing I wanted to add, because it happened to me and took me quit some time to find the error. I wrote alias myFolderShortcut = 'cd /Users/danielbeck/Documents' because I like to have spaces between my equal signs. But it gave me an error.

So after removing the spaces it worked.

So use

alias myFolderShortcut='cd /Users/danielbeck/Documents'

I hope this will help someone in the future.

Peter

Posted 2012-01-05T19:03:51.833

Reputation: 11

0

Another approach is to use the CDPATH environment variable.  This works for the cd command the way PATH works for running programs — if you type a cd command with a parameter that doesn’t begin with / (or ~), the shell looks for a directory by that name under each component of CDPATH.  For example, if you want quick access to the following directories:

  • /Users/benhowdle89/Documents/Projects/Project42
  • /Applications/MAMP/htdocs/website/WhoAt/solr/whoat
  • /the/quick/brown/fox/jumps/over/the/lazy/dog

then set CDPATH to the following value

:/Users/benhowdle89/Documents/Projects:/Applications/MAMP/htdocs/website/WhoAt/solr:/the/quick/brown/fox/jumps/over/the/lazy

and you’ll be able to use the commands cd Project42, cd whoat, and cd dog, etc.  The value begins with a colon (:) so as to include an initial null entry.  In some shells, this is necessary to get cd to look in the current directory first.

This should work in most POSIX-type shells; e.g., bash and ksh.  For bash, put a command like

export CDPATH=:/Users/benhowdle89/Documents/Projects:/Applications/MAMP/htdocs/website/…

into your ~/.bash_profile or ~/.bashrc.  (If you use ~/.bashrc, you may be able to leave out the export.)  For csh-type shells, use cdpath and the appropriate csh-style syntax.

G-Man Says 'Reinstate Monica'

Posted 2012-01-05T19:03:51.833

Reputation: 6 509