Making new Terminal windows automatically cd to the current Finder directory
I think this is my first post that has nothing to do with Safari extensions. I hope someone finds it useful.
Here’s a trick I figured out to make new Terminal windows (and tabs) automatically cd to the current Finder directory on opening. I don’t know that this is always a good idea, but to me at least, it doesn’t do any harm and is often helpful.
There are probably several ways to accomplish this, but the technique I use involves a combination of an AppleScript script and an entry in the .bash-profile file in your home folder.
Here’s the AppleScript in its entirety:
tell application "Finder"
if window 1 exists then
if target of window 1 as string is not ":" then
get POSIX path of (target of window 1 as alias)
end if
end if
end tell
I’ve uploaded the AppleScript to my Dropbox as get_finder_path.scpt in case you want to use it.
Once you have the AppleScript, put it somewhere on your internal hard drive (for speed of access). I put it in ~/Library/Scripts/Applications/Terminal/, but it doesn’t matter where you put it.
The next step is to insert one line in your .bash-profile. If you’ve never edited this file before, it probably won’t exist on your Mac, in which case you’ll have to create it. To find out whether it exists, open Terminal and enter the following command:
ls ~/.bash_profile
If Terminal says “No such file or directory”, enter this command to create the file:
touch ~/.bash_profile
Now, open the file for editing with the following command, optionally replacing “TextEdit” with the name of your favorite text editor.
open ~/.bash_profile -a TextEdit
Copy and paste the following line anywhere in the file, as long as it’s on its own line.
cd "`osascript ~/Library/Scripts/Applications/Terminal/get_finder_path.scpt`"
If you saved get_finder_path.scpt somewhere other than the indicated directory, edit the path appropriately. Finally, save the file.
If all has gone well, the next time you open a new Terminal window or tab, it should automatically cd to the folder that’s open in the frontmost Finder window, if any. If there is no Finder window open, or if the window is showing something other than a standard folder—for example, the “Network” folder, or another false folder—the Terminal’s working directory will remain your home folder.
One last thing—if you want a quick way to cd to the Finder’s current directory after opening a Terminal window, you can add another line to your .bash-profile as follows, again editing the path to the AppleScript file as necessary:
alias cf='cd "`osascript ~/Library/Scripts/Applications/Terminal/get_finder_path.scpt`"'
Then, in your next Terminal session, you’ll be able to jump to the current Finder directory by just entering cf. If you’d prefer a different shortcut, just replace “cf” in the above line with the word of your choice.

