markupboy

Terminal Tip - Copying Your Current Path to the Clipboard

Penned on .

I spend most of my day bouncing between the terminal and TextMate and often need the full path of where ever I happen to be in my shell. OS X comes with a great built-in terminal command - pbcopy - for just such occasions.

Copying your path to the clipboard is as easy as getting the present working directory and piping it to pbcopy

pwd | pbcopy

I found, though, that this leaves the carriage return at the end of what was copied. We can fix this using the translate characters command tr.

pwd | tr -d "\r\n" | pbcopy

This will remove all instances of the carriage return / line feed pair coming out of pwd and pass it to your clipboard. Of course, it's not the easiest thing to remember, so I've aliased it to the command getpath in my shell configuration

alias getpath='pwd | tr -d "\r\n" | pbcopy'

And that's it. Now when I'm in a terminal session I can simply run getpath and my location in the filesystem is copied right into my clipboard.

UPDATE: With a recent update, my getpath alias stopped working. For whatever reason, tr just stopped trimming pwds output correctly. Fortunately, I happened on an even easier way to grab the current path by piggy-backing on the $PWD environment variable. So here's the new and improved get path:

alias getpath='echo -n $PWD|pbcopy|echo "current path copied to clipboard"'