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

Control the Finder with the Terminal UNIX
There are several ways to open a Terminal window to the current directory in the Finder. But wouldn't it be useful if you could do the reverse and open a Finder window to the current Terminal directory? Well, you can, and you can completely control the Finder from the terminal.

I have put the code on github with full instructions on how to set it up. It works by using bash_completion, .bash_profile with some applescript to control the Finder, .inputrc and .bash_aliases.

Here are some of the features:
  • Changing a directory in the Terminal opens the same directory in the Finder.
  • You can change the Finder window view from the Terminal (column, list, icon views).
  • It is case insensitve, you can press Tab for menu completion, and Shift-Tab to expand bash aliases.
  • Open a Terminal directory to the current Finder window.
This code will work with both the Terminal.app and iTerm2 and should work with older macs as well

[kirkmc adds: I haven't tested this, but it sounds very useful.]
    •    
  • Currently 2.80 / 5
  You rated: 1 / 5 (10 votes cast)
 
[10,269 views]  

Control the Finder with the Terminal | 11 comments | Create New Account
Click here to return to the 'Control the Finder with the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Control the Finder with the Terminal
Authored by: Manevitch on Dec 13, '12 07:53:20AM

To open the current directory in the Finder, issue the command:

open .

Don't forget the period!

---
--
Lee



[ Reply to This | # ]
Control the Finder with the Terminal
Authored by: brianpratt on Dec 13, '12 08:38:34AM

If I'm not misunderstanding, you've always been able to "open a Finder window to the current Terminal directory":

When in a terminal window, just type: "open ." to open the current directory in Finder. You can, of course, do the same with a subdirectory: "open <subdir_name>" or any file, really. Typing "open <filename>" in the Terminal is just like (or very similar to) double-clicking on the file in Finder.

The rest of the functions added look interesting. Thanks for sharing!



[ Reply to This | # ]
Control the Finder with the Terminal
Authored by: Mr. H on Dec 13, '12 08:43:51AM

Hah! Snap!



[ Reply to This | # ]
Control the Finder with the Terminal
Authored by: NapoleonWils0n on Dec 13, '12 05:55:13PM

Heres a more accurate description
Changing directory in the Terminal changes the front most Finder window to the same directory

This isnt just opening a Finder window to the current Terminal directory like the open command
Or opening a Terminal window to the current Finder directory like go2shell

This code can do both of these and much more

When you cd in the Terminal to change directory the front most Finder automatically changes to the same directory

Ill try and explain how this code is different from the open command,
what it does and how it works

Heres how the open command works:

With the open command you can type: open Desktop in the terminal and this will open the Desktop in a new Finder window

If you already have a Finder window open, the open command will open the Desktop in a second Finder window

When you use the open command to open a directory in the Finder from the Terminal it will always open another Finder window, it doesnt change the directory of the front most Finder window.

eg if you type:

open Desktop
open Pictures
open Sites

You would end up with 3 Finder windows

Heres what the code does instead:

When you cd into a directory in the Terminal the front Finder window changes to the same directory, if a Finder window isnt open it will open one for you

The code works with .bash_completion and some options set in .inputrc like ignore-case

For example here's how you would change the front Finder window to your Music folder in ~/Music/iTunes/iTunes\ Media/Music/

cd m + tab 2 times
expands to: cd Music/

cd Music/i + tab once
expands to: cd Music/iTunes/

cd Music/iTunes/i + tab once
expands to: cd Music/iTunes/iTunes\ Media/

cd Music/iTunes/iTunes\ Media/m + tab 3 times
expands to: cd Music/iTunes/iTunes\ Media/Music/

You can then type: column, list or icon to change the Finder windows view

The CDPATH variable is set in the .bash_profile to the home directory, so you can cd back to any directory in your home quickly

If you type cd d + keep pressing tab it will cycle through all the directories begining with d
For example: Desktop, Documents, Downloads, Dropbox

rtfm
You need to download and install bash_completion in /etc



[ Reply to This | # ]
Control the Finder with the Terminal
Authored by: dzurn on Dec 18, '12 10:29:18AM
I use ZSH, so your iTunes example would get reduced to this with the builtin zsh completions:

cd ~/M/i/i 
then hit {tab} and it expands to:
cd ~/Music/iTunes/iTunes\ Music/

Not to take anything away from your code, since I can see it being useful to have a Finder window "follow" the current directory. However there's times I use the Terminal to navigate to folders with thousands of files that otherwise woule take a looong time to show in the Finder.

---
Madness takes its toll.
Please have exact change.


[ Reply to This | # ]
Control the Finder with the Terminal
Authored by: googoo on Dec 14, '12 05:05:59AM
Nice! I have had the following alias in my .bashrc file for a long time:
 alias finder='open -a Finder .'
Yours is even shorter.

-Mark

[ Reply to This | # ]

Control the Finder with the Terminal
Authored by: Mr. H on Dec 13, '12 08:42:44AM

With reference to the opening sentences of the hint: "There are several ways to open a Terminal window to the current directory in the Finder. But wouldn't it be useful if you could do the reverse and open a Finder window to the current Terminal directory?"

I'm sure plenty of people already know this, but for those that don't, typing:

open .

(that's "open", space, dot) and pressing return in Terminal will open the current terminal directory in Finder.



[ Reply to This | # ]
Control the Finder with the Terminal
Authored by: MacUser06 on Dec 13, '12 01:11:55PM

Open is a fairly advanced command, as a friend of mine put it, it is heavily modded.

It can amongst other things take a stream and open it in TextEdit, it can make the foreground window of an app come to front. It can open an app in the background, it can also open specific apps..

A read of its man page is recommended: "man open" in a Terminal window.

This brings the Finder window for CWD (current working directory) to front from the Terminal window:

open -b "com.apple.finder" .

Observe the dot at the end of the command!

Edited on Dec 13, '12 01:22:59PM by MacUser06


[ Reply to This | # ]
Control the Finder with the Terminal
Authored by: stib on Dec 14, '12 05:04:59AM
A very cool use ofopen is when you use the -n option to open an application. It opens a new instance of an application whether or not another one is already running. BTW don't follow the installation instructions on the github page if you have customised your ~/.bash_profile file, as it completely overwrites it, and none of the stuff in it is actually relevant.

[ Reply to This | # ]
Control the Finder with the Terminal
Authored by: Lri on Dec 13, '12 11:07:20PM

Here's a simplified function that just makes cd change the directory in Finder.

function cdee() {
	[ $# == 0 ] && \cd ~ || \cd "$1"
	[ $? != 0 ] && return
	osascript -e 'on run pwd
set f to posix file pwd
tell app "Finder"
if number of Finder windows is 0 then
open f
else
set target of window 1 to f
end
end
end' "$PWD" > /dev/null
}

alias cd=cdee


[ Reply to This | # ]
Control the Finder with the Terminal
Authored by: wgscott on Dec 14, '12 09:57:38PM
Welcome to 2003 http://hints.macworld.com/article.php?story=20030915193326598

[ Reply to This | # ]