Copy the Current Path from Terminal to the Clipboard in Mac OS X

Jun 18, 2014 - Leave a Comment

Copy the current path from the Terminal into the Clipboard of 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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates