Open new Terminal window in current (or other specified) directory

A lot of times I find myself wanting to open another (Mac OS X) Terminal window in the same directory as my current one. This little shell script, which executes a little AppleScript, makes that trivial:

#!/bin/sh

if [ $# -ne 1 ]; then
    PATHDIR=`pwd`
else
    PATHDIR=$1
fi

osascript -e "tell application \"Terminal\"" -e "do script \"cd $PATHDIR\"" -e "end tell"

Save this script somewhere in your $PATH with executable permissions. Now instead of hitting Command-N then typing “cd really/long/path/to/your/current/directory”, you can simply type the name of the script (I used “term”):

term

Chain them together to open multiple windows. The following would open three new windows with the same current directory:

term; term; term

It can also take an optional directory path argument to override the current directory:

term /System/Library/

  • http://andr.esmejia.com Andrés Mejía

    Nice, but is there a way to bind it to the Command-T shortcut?

  • Anonymous

    This code ins’t clean or good or DRY but it does work and I don’t have any more time to mess with it. This will do the same thing but in a new tab instead of a new window.

    #!/bin/sh

    if [ $# -ne 1 ]; then
    PATHDIR=`pwd`
    else
    PATHDIR=$1
    fi

    osascript -e “Tell application “System Events” to tell process “Terminal” to keystroke “t” using command down” -e “Tell application “System Events” to tell process “Terminal” to keystroke “cd $PATHDIR”” -e “Tell application “System Events” to tell process “Terminal” to key down return” -e “Tell application “System Events” to tell process “Terminal” to keystroke “clear”” -e “Tell application “System Events” to tell process “Terminal” to key down return”

  • ryanE

    This script is working, mostly, but it isn’t working when I try to run it from certain folders. I tried to run it from a git (which has a pretty long path name) and got an error for cd: that said the pathway didn’t exist. But the pathway that it listed (that didn’t work) was only part of the pathway to the directory that I’d attempted to run the script in. It ends up opening a new window in my home directory, instead. Any advice?