Finder and Terminal

Here we'll show you how to move the present working directory of a Terminal window to the current directory in the front most Finder window.

I'm sure you're aware of the terminal command

open .

which opens a Finder window in the current directory of your Terminal window. And of course there are equivalent plugins to open a terminal window from finder:

Both of them allow you to place a Finder Toolbar button to open a Terminal window.

But my new favourite is a script that changes your present working directory to that of the front most Finder window. Thanks to users at stackoverflow for this. This rocks for jumping around the file structure, especially if you've just browsed somewhere through finder and need to run a script in the directory.

Drop these functions into your .bash_profile using vim or other:

  1. function ff { osascript -e 'tell application "Finder"'\
  2. -e "if (${1-1} <= (count Finder windows)) then"\
  3. -e "get POSIX path of (target of window ${1-1} as alias)"\
  4. -e 'else' -e 'get POSIX path of (desktop as alias)'\
  5. -e 'end if' -e 'end tell'; };\
  6.  
  7. function cdff { cd "`ff $@`"; };
  8.  

Then you can type cdff to change to the front most finder directory. Awesomely powerful!

Happy Terminaling...

Published by

Michael

http://michaelarchbold.com

Leave a Reply

Your email address will not be published. Required fields are marked *