DEV Community

Cover image for Terminal: Copy Current Path
Brandon Charest
Brandon Charest

Posted on

Terminal: Copy Current Path

TLDR:

Create this alias into your .aliases/.zshrc/.bashrc or where ever you store aliases for easier use later.

alias cpwd="pwd | tr -d '\n' | pbcopy && echo 'pwd copied to clipboard'"
Enter fullscreen mode Exit fullscreen mode

Recently I have been spending a little more time using the terminal for work and have had a few instances where I needed to copy the current directory I am in. Now in most cases you could just use the mouse to highlight and copy the directory you need, but I am pretty lazy motivated to try and make this a little easier as I suppose there could be a time where using a mouse is not an option.

Parts Breakdown

pwd: print working directory

$ pwd
/User/(username)
Enter fullscreen mode Exit fullscreen mode

Prints the current directory path you are in.


tr: translate characters

tr -d '\n'
Enter fullscreen mode Exit fullscreen mode

This will copy standard input to the standard output with substation or deletion of selected characters.

The pwd command above will produce the working directory string but it will have a line return character '\n' appended to it.

We use the -d command to signal that we want to delete a character off the string and '\n' to signal which character we wish to delete.


Copying to system clipboard

Depending on if you are on OSX or Windows the copy command could be different

Windows

  • clip

OSX

  • pbcopy

this will copy text to the system clipboard (by default).


echo: Writes to the standard output.

echo 'pwd copied to clipboard'
Enter fullscreen mode Exit fullscreen mode

This part is not needed I just personally like to have something in my commands that will tell me that the command was at least executed.


Pipe it all together

All of these above commands are used with a pipe '|' which will feed the 'output' of one command to the next.

So the flow will be the following.

  1. pwd prints out the working directory
  2. That output is now fed to our tr argument which will remove the trialling newline character
  3. finally that entire string will be placed into our clipboard for use with the help of pbcopy/clip.
  4. Then an echo at the very end will just print to the screen that the command has been executed

Final

Now this is far from the only way to do this, I believe a popular way would be to just use xclip but that may take an install and I tried to find a way to use only what is available by default.

I hope that someone may find his useful. What are some of the things you have learned in the terminal that has saved you time/made things easier? Let me know! đŸ˜ƒ

Top comments (7)

Collapse
 
fm profile image
Fayaz Ahmed

You don't need the tr command. Just use the $PWD variable like this:

echo -n $PWD | pbcopy
Enter fullscreen mode Exit fullscreen mode
Collapse
 
brandoncharest profile image
Brandon Charest

Good to know! Thanks for sharing I should have known there was an easier way.đŸ˜ƒ

Collapse
 
fm profile image
Fayaz Ahmed • Edited

Yeah. It's the -n option of echo that removes the newline.

Collapse
 
raccoon1515 profile image
Alexander Ilyin

Nice. But what clipboard copy analog for Ubuntu / Deb based distributives?

Collapse
 
brandoncharest profile image
Brandon Charest • Edited

I think a common solution would be to use xclip, but that may have to be installed, I am not positive if that comes along by default, and I do not know what the alternative to clip/pbclip for Ubuntu/Deb if there is one. But the command should still be relatively similar.

pwd | tr -d '\n' | xclip && echo 'pwd copied to clipboard
Enter fullscreen mode Exit fullscreen mode
Collapse
 
goncalorodrigues profile image
Gonçalo Rodrigues

Yea unfortunately it doesn't come by default, but I do recommend installing it, it's very useful in my day to day

Collapse
 
moopet profile image
Ben Sinclair

I think it depends whether you're using X, doesn't it :)

So in an SSH session to another device, a virtual terminal, VM, a Docker container, WSL, or any of the other places people use Linux, it won't work.

I suspect most people who use a Windows or MacOS machine as their daily driver doesn't have X installed on any of the things they use Linux on.