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.

2 Answers 2

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.

1
  • pbcopy is just what I need, will accept this answer as soon it will become possible, thank you.
    – eiennohito
    Jul 29, 2013 at 14:13
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .