July 25, 2015

Bash Shortcut: Copy your Present Working Directory to your Clipboard

edit

Like most developers, I’m always looking for ways to increase my efficiency. One way I like to do this is through Bash functions.

Just like other programming languages, Bash functions allow you to combine Bash commands or manipulate user input to output a desired effect. While I have a good number of Bash functions, today we’re going to focus on a simple one: how to copy your present working directory to your clipboard.

While in your terminal, the present working directory (or pwd) is the directory you are currently in. Using the command pwd will supply you with an output of your current directory.

The other Bash command we’re going to use is the pbcopy command. This command is short for “pasteboard copy” and will take the standard input and paste it on the clipboard. A nice feature of the pbcopy command is the ability to “pipe” (|) the standard output of one command into the pbcopy command. We’re going to use that to make our Bash function. Put this in your .bash_profile or .bashrc file.

# Copy the PWD to the Clipboard
alias cpwd="pwd | tr -d '\n' | pbcopy && echo 'pwd copied to clipboard'"

Now we can use the command cpwd to copy our present working directory to the clipboard. I use this frequently for getting the right file paths for symlinks, but there are other uses as well.

Hope this helps!


Liked the post?
Give the author a dopamine boost with a few "beard strokes". Click the beard up to 50 times to show your appreciation.
Need help with your software problems?

My team and I are ready to help you. Hire Agathist to build your next great project or to improve one of your existing ones.

Get in touch
Kyle Shevlin's face, which is mostly a beard with eyes

Kyle Shevlin is the founder & lead software engineer of Agathist, a software development firm with a mission to build good software with good people.

Agathist
Good software by good people.
Visit https://agath.ist to learn more
Sign up for my newsletter
Let's chat some more about TypeScript, React, and frontend web development. Unsubscribe at any time.
Logo for Just Enough Functional Programming
Just Enough Functional Programming
Check out my courses!
If you enjoy my posts, you might enjoy my courses, too. Click the button to view the course or go to Courses for more information.