This is Bruce from Tennessee, aka UseTheData, with a tip for working with the Finder and Terminal.
The problem to be solved is that, when working with files, I find some operations to be easier in the Finder and others to be easier in the Terminal. There are times where I needed a terminal window open to the same directory as my current Finder window. And there are also times, when working in an existing Terminal window, where I want to either have a Finder window open in that same directory or get my Terminal window into the same directory as an existing Finder window.
As Allison reminded me, we’ve seen some ways to do part of this from the Finder. As explained in an October 2021 Tiny Tip by Helma, the path bar in Finder is powerful, and you can use the control-click on a folder in the Path Bar to open a Terminal Window in that particular directory. So, that solves one of the three cases. But, how can I deal with the other two cases?
The answer is a bit of shell script work, which I’ll describe in a minute. But let me first describe the results.
When I’m in a terminal window on my Mac, I can simply type cdf
— short for Change Directory to Finder — and that terminal session will now be in the same directory as the frontmost Finder window. Going the other way, I can type fhere
— short for Finder Here — and I get a new Finder window opened up in the same directory as my terminal session.
So, if that’s not useful for you, then feel free to fast forward to the next chapter in the podcast or move along to another blog entry.
I’ve had this capability for a long time, but in Allison’s call for some content, it occurred to me to share this with the Nosillacastaways. The answers came from Brett Terprstra in a blog post that is just under a decade old now. I was pretty sure that this came from Brett, and his site, like Allison’s, has a great search feature, so finding the post wasn’t that hard. However, several things have changed about macOS since 2013, and some of those changes affect what we need to do to make this work in a current version of macOS.
When Brett wrote his article, macOS used something called bash
as the default shell in the terminal. Starting in 2019 with macOS 10.15 Catalina, the default shell is now something called zsh
. For a very large fraction of the things that a user does in the terminal, there’s no difference. But, if you want to customize your environment, the differences are significant.
For more about bash
, zsh
, and other shells, go all the way back to Taming the Terminal Part 1 of n. Bart and Allison also provide a lot of information about customizing your terminal environment in Taming The Terminal Parts 13 and 14.
Over in Programming by Stealth, the dynamic duo have also provided great information on these configuration files and managing them, particularly in episodes 121 Managing Dot Files and an Introduction to Chezmoi and 122 Managing Dot Files with Chezmoi.
If you’ve set up a new user account at some point starting with macOS Catalina, you are most likely using zsh
and Brett’s instructions won’t work for you. If you’ve carried forward a user account, either with Migration Assistant on a new Mac, or because Macs last a long time and you’re running a Mac that came out of the box with something before 10.15 Catalina, you’re probably using bash
and you can just use Brett’s instructions to set up the commands.
How do you tell whether you’re using the new zsh
or bash
? There are a few different ways to do this, but a simple way that’s worked every place I’ve tried it is to type echo $0
in a Terminal session, followed by a return. The result will tell you if you’re running zsh
or bash
. If it says bash
, just follow Brett’s instructions and you’re good.
If you’re running zsh
, then you need to edit your .zprofile
file, rather than the .bash_profile
file that Brett describes, and the incantation for the cdf
command is slightly different.
Bart and Allison describe editing text files, like .bash_profile
and .zprofile
from the Terminal in Taming the Terminal Part 11 of n using the nano editor. Following that guide, while working in a terminal, you can type
nano ~/.bash_profile
if you’re running bash
and
nano ~/.zprofile
if you’re running zsh
. The tilde and slash are there to make sure you’re editing the file that’s in your home directory, regardless of what directory you happen to be running at the moment.
For bash
, copy paste in the lines below. I’ve used a slightly different abbreviation for “open a Finder window here” than Brett did, so there’s a slight difference from his blog post.
# cd to the path of the front Finder window
cdf() {
target=`osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)'`
if [ "$target" != "" ]; then
cd "$target"; pwd
else
echo 'No Finder window found' >&2
fi
}
# Open a new Finder window in the current working directory
alias fhere='open -a Finder ./'
If you’re running zsh
, then copy and paste the lines below:
# change to the directory of the frontmost finder window
cdf() {
target=`osascript -e 'tell application "Finder" to get POSIX path of (target of front Finder window as text)'`
cd "$target"
}
# Launch a finder window set to the current working directory
alias fhere='open -a Finder ./'
If you’re not sure where to put these lines in the file, just add it to the end. Then save the file using control O
, quit nano with Control X
, quit the Terminal app, and launch a new Terminal window. The cdf
and fhere
commands should now work for you.
The way to create fhere
is the same for the two shells, but the syntax to create the cdf
command is a bit different. For those interested, that cdf
command is invoking a bit of AppleScript to get the directory for the frontmost Finder windwo. It’s also interesting to me as a command line aficionado as being an example of using all three types of quotes in one line. Bart and Allison go into two of the three types of quotes in PBS 143 of X — Shell Script Basics. The third type of quote, the so-called backtick, which is above the tab key on my US keyboards, is there to tell the shell to execute this command and give me the results as a string.
I hope you’ve found this to be a useful tip, and my thanks to Brett Terpstra for figuring all of this out, and to Bart and Allison for their great work in both Taming the Terminal and Programming by Stealth.
Peace, and may you find beauty in the world around you.
I like to live in terminal; these are very useful zsh functions – I like ’em. I was using drag & drop of directories in finder to a “cd ” in terminal (cdf) and navigating in finder to a terminal directory (fhere). Been doing that for years – this is quicker – the judges like quicker.