How to copy full path from Terminal in Mac OS

2

I want to copy path of one file, but the directory isn't open in Finder, it is opened in terminal. Is it possible by invocation <utility> filename to copy full file path string to the Mac OS clipboard? Like copying the output of the following command there.

`pwd`/filename

Added the following line to my .zshrc

function cn { echo -n $(pwd)/$1 | pbcopy }

Just aliasing don't work because of arguments. Exact same string should work in bash too.

eiennohito

Posted 2013-07-29T14:01:13.607

Reputation: 547

Answers

5

echo $(pwd)/filename | pbcopy

This will give you a non-quoted and non-escaped string, which, depending on your use case, may not work properly when pasted elsewhere.

slhck

Posted 2013-07-29T14:01:13.607

Reputation: 182 472

pbcopy is just what I need, will accept this answer as soon it will become possible, thank you. – eiennohito – 2013-07-29T14:13:45.187

0

If you are willing to install greadlink with brew: brew install coreutils, you could implement a solution that works for any path (file in another folder or a folder name). This is what I have in ~/.zshrc:

function path_to_clipboard() {
    local path_escaped="$(printf "%q\n" "$1")"
    local path_escaped_full=$(greadlink -f "$path_escaped")
    echo -n $path_escaped_full | pbcopy
}
alias pb=path_to_clipboard

Juuso Ohtonen

Posted 2013-07-29T14:01:13.607

Reputation: 475