Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to create a command like cd - (lets call it cdp) that will change directories to the last changed-to directory from another terminal window similar to the option to open a new terminal in the directory that previous window/tab was in (I don't see that option in Mac OS X terminal).

To do so I figure I could alter cd with something like alias cd='cd $1;echo $PWD > /tmp/CWD' and then add

alias cdp='cd  `cat /tmp/CWD`

Can someone key in a better solution? Or, fill me in on an existing program, feature, etc., please let me know. On Mac OS X 10.6 with default terminal. Thanks.

share|improve this question
up vote 4 down vote accepted

Aliases don't accept parameters. You'll have to use a function. You should also use the command builtin.

function cd () { command cd "$@"; echo "$PWD" > /tmp/CWD; }

alias cdp='cd "$(</tmp/CWD)"'

See this for another approach that's specific to OS X. It's a script that can launch a new Terminal window or tab opening with its current directory the same as that of the current Terminal window or tab.

share|improve this answer
Thanks for correcting my method. – physicsmichael Oct 12 '09 at 3:46
That will break with spaces in the filename, won't it? You'd need cdp='cd "$(</tmp/CWD)'". Or set your cd override to ln -sf "$PWD" /tmp/CDW, and cdp='cd -P /tmp/CWD'. This is still clunky and only works for one directory, even if you have multiple terminals CDed to multiple places. (most recent cd wins, not most-recently-used terminal). I guess Mac OS's Terminal app doesn't have an option to open new tabs / windows with the same CWD as the child process in the currently-focused terminal, like gnome-terminal does. – Peter Cordes Feb 6 '15 at 13:34
@PeterCordes: Thanks for spotting that omission. – Dennis Williamson Feb 6 '15 at 15:29
Hooking cd, instead of PROMPT_COMMAND='pwd > /tmp/"CWD.$USER"' is a nice idea, though. I used it for… – Peter Cordes Feb 6 '15 at 17:27

I like the solution at better:

Open new xterm windows in current Terminal directory Authored by: TomP on Jan 04, '06 10:05:12PM

As a slightly more flexible alternative, I have a little shell script ("") that will open a new Terminal window in the same directory as the Terminal session from which it was invoked. Here's the script:

# Open another terminal window for the current directory
# Copyright 2004 by Tom Pollard - All rights reserved.
#set -x
set thePath to "$CWD"
set myPath to (POSIX file thePath as alias)
    tell application "Terminal"
        do script with command "cd \"" & thePath & "\""
    end tell
end try

I have this aliased as 'ot'. So, when I want another Terminal window open to the same directory as some other Terminal window (not necessarily the last one I used or opened, I just say 'ot' in that Terminal session.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.