5

Possible Duplicate:
How to build a custom command with options

I want to create a terminal short that takes me to a deeply-nested folder. For example, I just want to type in "star" every time I open the terminal to take me to cd source/alpha/beta/star.

I find it hard to believe this question hasn't been asked, but I can assure you haven't found the exact step-by-step solution. If you could let me know how to, or just point me to an existing answer, I'd appreciate.

1
  • ... unless someone remembers that, unlike in that case, aliases aren't the only possible answer here, and tells the poor questioner about the CDPATH variable. (-:
    – JdeBP
    Jun 19, 2011 at 12:16

4 Answers 4

5

You need to look at bash aliases.

Try adding the following to either your .profile or .bashrc or .bash_profile:

alias star='cd source/alpha/beta/star'

Any command-line editor (eg vi or emacs) will work, as would any text editor for the Mac. Aliases work on all Unix-like environments (SunOS, Mac OS X, Linux, AIX, etc).

Then exit Terminal, and when you restart, it should be there. (Alternatively, you can source the script file, but I've found restarting bash to be better.)

2
  • Can you please point out where .profile, .bashrc and .bash_profile are located? Thanks
    – Paul Mason
    Apr 17, 2013 at 22:18
  • @PaulMason - they're where they are in any *nix-like environment: in your home directory when you open your console.
    – warren
    Apr 18, 2013 at 11:37
3

You want to make aliases. Here's an easy way to make an alias without having to open up the .bashrc file. Just save this script into a file called mkalias and then move it to your usr binary directory (I believe that is ~/bin for OSX). Then run chmod +x mkalias on the file. Now whenever you want to create an alias you just type: mkalias myalias='my commands'.

#!/bin/bash
# path to .bashrc, .zshrc, etc.
export RC='~/.bashrc'

if [ `expr index "${1:-0}i" =` == 0 ]; then
    echo "Usage: mkalias ALIAS=EXPRESSION
Makes alias permanent by writing to .bash_aliases contained in home directory."
else
    # Export alias
    echo 'alias '$1 >> $RC
fi

and to answer your question now, just type:

alias star='cd /path/where/you/want/to/go'
1

Try this applescript on for size:

do shell script "open -a Terminal.app"
tell application "Terminal"
    do script "cd /path/to/my/directory"
end tell

That should open a Terminal for you and take you into your directory. You can shortcut it to your desktop, or maybe create a shortcut for it.

0

Shell scripts are the normal way to get things done, but cd only affects the current shell and shell scripts invoke a new subshell in order to run. You will need to create an alias in order to affect the current shell.

alias star="cd source/alpha/beta/star"

If you add that line to ~/.bashrc then it should normally be available whenever you open the terminal.

3
  • how to you add to ~/.bashrc?
    – keruilin
    Jun 19, 2011 at 3:21
  • Open it in a text editor. It's a plain text file. Jun 19, 2011 at 3:26
  • The . indicates that the file is hidden, so you can enable hidden files in your finder, or ls -a to see what's there. Edit as any other text file. Jun 19, 2011 at 3:35

Not the answer you're looking for? Browse other questions tagged .