Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automatically cd to active Finder directory in new Terminal window UNIX
There exist small apps that will open a new Terminal window or tab and automatically cd to the directory of the active Finder window. This hint describes a way to have the Terminal always cd to the directory of the active Finder window whenever you open a new Terminal window or tab -- without the use of an app or any other third-party solution.

In other words, whenever you open a Terminal window or tab, if there is an open Finder window, the Terminal will start in the directory shown in the Finder window.

To make this happen, all you need to do is add the following line somewhere in your .bash_profile file in your home directory:
cd `osascript -e 'tell application "Finder" to 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)'`
To edit your .bash_profile file, open Terminal and enter the command:

open ~/.bash_profile -a TextEdit

Paste the code on an empty line anywhere in the file, and save the file. If Terminal complains that .bash_profile doesn't exist, create it with this command:

touch ~/.bash_profile

Then edit it as described.

If you want a quick way to cd to the active Finder directory manually, add the following code to your .bash_profile:
alias cdf='cd `osascript -e "tell application \"Finder\" to 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)"`'
Upon opening a new Terminal window, you'll be able to cd to the active Finder directory by just entering cdf at the prompt. If you'd rather use a different shortcut, substitute your own string in the above code, after 'alias ' and before the equals sign.

(The alias hint is similar to this hint, but discovered independently.)

Note: If you use the 'cdf' alias when there is no open Finder window, or when the active Finder window is showing something other than a directory, you will be taken to your home directory.

[crarko adds: I tested this, and it works as described.]
    •    
  • Currently 4.00 / 5
  You rated: 3 / 5 (8 votes cast)
 
[6,410 views]  

Automatically cd to active Finder directory in new Terminal window | 14 comments | Create New Account
Click here to return to the 'Automatically cd to active Finder directory in new Terminal window' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatically cd to active Finder directory in new Terminal window
Authored by: 4nd! on Sep 23, '11 08:09:01AM

Heyho...

that's really a cool idea and a useful hint! Nevertheless it does not work with directories that contain spaces in the name. E.g. when I'm in the folder in "/Users/me/Documents/eclipse workspace" the script tries to move to "/Users/me/Documents/eclipse"...and that's wrong.

Could you fix it in both scripts? Remark: I only tested the second one, eventually the first one works.

Best,

Andi



[ Reply to This | # ]
Automatically cd to active Finder directory in new Terminal window
Authored by: mike cupcake on Sep 23, '11 08:11:07AM

Very useful but needs tweaking to work with volume/folder names that include spaces



[ Reply to This | # ]
Automatically cd to active Finder directory in new Terminal window
Authored by: TonyT on Sep 23, '11 08:33:58AM

In Lion, just enable the a new service, and then just right-click and select "New Terminal at Folder"

Edited on Sep 23, '11 08:34:47AM by TonyT



[ Reply to This | # ]
Go2Shell
Authored by: redfood on Sep 23, '11 08:43:04AM

Or just download Go2Shell from the app store (it is free).



[ Reply to This | # ]
Automatically cd to active Finder directory in new Terminal window
Authored by: Lri on Sep 23, '11 09:09:29AM

The path should be quoted:

cdf() { cd "`osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)'`"; ls; }

Edited on Sep 23, '11 09:44:36AM by Lri


[ Reply to This | # ]
Automatically cd to active Finder directory in new Terminal window
Authored by: googoo on Sep 23, '11 09:59:23AM
I do something similar, except I put in an extra condition in case the selection of the finder window errors. This could occur if there are no finder windows open. My cdf script is

tell application "Finder"
	try
		POSIX path of (target of window 1 as alias)
	on error
		POSIX path of (path to desktop folder as alias)
	end try
end tell
I put this in a file named cdf.scpt (or whatever you like), and added the following line to my .bash_profile file

alias cdf='cd "`osascript path/cdf.scpt`"'
where path id the UNIX path to the location of the cdf.scpt file.

-Mark

[ Reply to This | # ]

Automatically cd to active Finder directory in new Terminal window
Authored by: Lri on Sep 25, '11 10:35:09AM

insertion location also returns the desktop if there are no open windows.



[ Reply to This | # ]
Automatically cd to active Finder directory in new Terminal window
Authored by: highbulp on Sep 23, '11 11:40:55AM

Doesn't seem to work for directories with spaces in their name (e.g., "My Pictures"). Can you modify the script language to include appropriate quotes? I've been using a Finder service, but being able to cdf from the terminal would make my life much, much easier :)



[ Reply to This | # ]
Automatically cd to active Finder directory in new Terminal window
Authored by: mlm on Sep 27, '11 08:57:57AM
When getting a POSIX path to use in the context of a Unix command, it should be surrounded by quotes, which is accomplished as follows:
    get the quoted form of the POSIX path of ...


[ Reply to This | # ]
Automatically cd to active Finder directory in new Terminal window
Authored by: ioioi on Sep 23, '11 07:50:17PM
This is a poor method to use, since clicking on any other window in Finder prior to entering Terminal becomes the path, resulting in an error:
$ cdf
execution error: Can’t make «class fvtg» of window 1 of application "Finder" into type string. (-1700)
A more effective way is like this, which also displays the path:
# cdf: cd's to frontmost window of Finder
cdf () 
{
    currFolderPath=$( /usr/bin/osascript <<"    EOT"
        tell application "Finder"
            try
                set currFolder to (folder of the front window as alias)
            on error
                set currFolder to (path to desktop folder as alias)
            end try
            POSIX path of currFolder
        end tell
    EOT
    )
    echo "cd to \"$currFolderPath\""
    cd "$currFolderPath"
}
resulting in:
$ cdf
cd to "/Users/ioioi/Documents/..."
orion:Documents ioioi$ 
from: http://hayne.net/MacDev/Bash/aliases.bash

[ Reply to This | # ]
Mine never fails
Authored by: juanfal on Sep 24, '11 03:16:49AM
In .bash_profile put:

cdf () {
  theFinderPath=`osascript <<END
tell application "Finder"
	try
		set thePath to get the POSIX path of (folder of the front window as string)
	on error
		set thePath to get the POSIX path of (path to desktop folder as string)
	end try
end tell
END
`
  cd "$theFinderPath";
}


[ Reply to This | # ]
how to do this in tcsh (.tcshrc)
Authored by: Dave1439 on Sep 24, '11 03:25:03AM
Can anyone advise how I might make the cdf idea work in tcsh? I've tried a few derivations and am getting various errors including:

unmatched ".

and

badly placed ().

I'm not a complete beginner but I wouldn't say I'm any kind of expert in the terminal, so my apologies if there's anything obviously stupid I'm doing here. ;)

[ Reply to This | # ]
Automatically cd to active Finder directory in new Terminal window
Authored by: gopes on Sep 24, '11 08:19:35AM

You can also go to Terminal's Preferences > Settings and choose the Shell tab, which gives you an option to run a command when the shell starts, and paste the line in that field. It won't affect anything other than Terminal, which could either be good or bad… I'm not sure, but I think adding it to your .bash_profile would change your directory even on remote login, which wouldn't make much sense, since you aren't using the Finder. (I'm probably going to use the command alias method, personally.)



[ Reply to This | # ]
Automatically cd to active Finder directory in new Terminal window
Authored by: Frankus on Oct 07, '11 01:38:27AM
Good point!

I use the following when I don't want code to run if I'm logged in remotely (say, via ssh):

if [ "$TERM_PROGRAM" == "Apple_Terminal" ]; then
  ...
fi


[ Reply to This | # ]