Copy the Current Path from Terminal to the Clipboard in Mac OS X
While it’s fairly easy to copy a folder path from the Mac GUI and Finder, or even to copy the path into the Terminal with a drag & drop trick, going the other direction and getting the current path from the command line and then having it accessible to the broader OS X clipboard is a little trickier… well, at least until you know this handy little tip.
This trick is quite simple in function, using the pwd command (short for present working directory) and the pbcopy command (a command line interface to the copy to clipboard function in OS X), at it’s most simple it works like this:
pwd|pbcopy
This will instantly copy the present working directory to the clipboard of OS X.
If you’re already familiar with the command line, you’re good to go, but for those who aren’t as knowledgable about the command line, let’s review this command sequence a bit more so that it makes more sense.
If you want to follow along, just launch Terminal app. First, we’ll want to be at the location within the command line to copy the path for. For the purpose of this walkthrough we’ll choose “/System/Library/CoreServices/Resources/” because it’s a deep(ish) system path that is universal on all Macs. Now at the command prompt, enter the following command:
cd /System/Library/CoreServices/Resources/
Hit the Return key and you’ll be in that folder, let’s verify it by using the aforementioned ‘pwd’ command:
pwd
Again, hit return, and you should see output like this:
$ pwd
/System/Library/CoreServices/Resources/
Now that you know you’re in the right place, let’s copy that directory path to the clipboard, but without having to manually select it using the mouse cursor and hitting Command+C, by using pbcopy instead:
pwd|pbcopy
How this works is simple: the ‘pwd’ command executes, then uses what’s called a ‘pipe’ to redirect the output of pbcopy into the next command, which in this case is ‘pbcopy’. As mentioned, pbcopy is a command line interface to the Mac OS X clipboard, thus by piping command output there, that data gets stored in the Macs clipboard. Not sure about that? Just open any text document, or even stay at the terminal prompt, and hit Command+V… you’ll see “/System/Library/CoreServices/Resources/” as the output. Excellent huh? You can also use the other end of pbcopy, pbpaste, to reveal the stored clipboard data.
If you plan on using this often, you could always make an alias for it within your profile by adding a line like this to .bash_profile:
alias copypath='pwd|pbcopy'
With that saved in bash_profile, you can just type ‘copypath’ and achieve the same effect.
This trick makes retrieving a current path simple, and eases going from the terminal to the GUI considerably. Remember, Mac users can also go the other way – from the GUI to the terminal – with an excellent drag & drop trick to automatically type out a full item path or file name from the Finder into the command prompt.
If you want to remove the newline character from the end of the string, then you can do this:
alias cpwd=”pwd|tr -d ‘\n’|pbcopy”
or this for no newline (either way):
echo -n `pwd`|pbcopy
# you could always make an alias for it within your profile by adding this to .bash_profile:
# cd to frontmost open finder folder
cdf() { cd “`osascript -e ‘tell app “Finder” to POSIX path of (insertion location as alias)’`”; pwd; }
# Copy the frontmost open finder folder Path from Terminal to the Clipboard in Mac OS X
cpf() { echo “`osascript -e ‘tell app “Finder” to POSIX path of (insertion location as alias)’`”|pbcopy; }