How To Copy The Current Directory’s Path To The Clipboard In A Single Terminal Command In Unix

When working from the terminal, we might need from time to time to copy the current path to the clipboard, for example to paste it in a text file. In this post we’ll show how to copy the current directory’s path to the clipboard in a single terminal command in a Unix system. Specifically, we’ll see the necessary commands when working either with:

  • Git Bash for Windows
  • MacOS

The quickest way to get the current path from the terminal line is by using the pwd command. The current path will be printed on the console output. This command works in all Unix systems.

A simple solution to copy the path to clipboard would be to select the path from the console output by clicking and dragging, then copy to clipboard with the corresponding hotkey combination, i.e. CTRL+INS when using Git Bash for Windows or COMMAND+C in MacOS.

Using the pipe

To avoid using the mouse, the output of the pwd command can be piped through a command that will copy the text content to the clipboard. This command depends on the system you’re working with:

  • pwd | clip when using Bash for Windows
  • pwd | pbcopy when using MacOS (e.g. working from zsh)

Now the current path will not be printed on screen, it will be copied to the clipboard. After running the command, open any text editor and launch the paste command. The path from where you launched the pwd command will be pasted in the text editor.

Get rid of the newline character

You may notice that the path will be terminated by a newline character. In fact, the cursor will be placed at the beginning of a new line after the newly pasted path.

You’ll likely want to just paste the path, without the newline automatically inserted right after it. To get rid of the newline character we need an additional stage. We will pipe the output of the pwd command through the tr command, like this:

  • pwd | tr -d '\n' | clip in Bash for Windows
  • pwd | tr -d '\n' | pbcopy in MacOS

The -d flag will modify the text by removing all occurrences of the character between single quotation marks, i.e. the newline character \n.

You can try to run the command above then paste the clipboard content in your text editor of choice. You’ll see that no newline will be inserted after the path that has been pasted.

Wrap it all up in a custom function

Now that we found the command we need, we might want to find a quicker and easier way to access it. We’ve already seen, in a previous post, how to create custom aliases and functions. We’ll create a custom function called cpwd. The name is a combination of cp and pwd, it’s short enough to be typed quickly and easy to remember.

The steps to follow are:

  1. Open the file ~/.bash_profile in your text editor of choice. Create the file if it doesn’t already exist.
  2. Add the following lines:
  • in Git Bash for Windows
  function cpwd() {
    pwd | tr -d '\n' | clip
  }
  • in MacOS
  function cpwd() {
    pwd | tr -d '\n' | pbcopy
  }
  1. Save the file and close the editor

If you are working from zsh in MacOS you’ll also need to create a file in the user root folder called .zshrc with the following content:

source ~/.bash_profile

Now if you start the a new terminal session, all you need to do to copy the current path to the clipboard is type cpwd and hit ENTER.

Conclusion

We’ve seen how to copy the current directory’s path to the clipboard in a single terminal command in a Unix system. We’ve seen in detail the combination of commands to use in Git Bash for Windows and in MacOS. The commands are the same except for the last one, which copies the content to the clipboard. Since the combination can be quite long to type, we can take advantage of custom bash functions to be able to quickly invoke it.