Bash one-liner: Copy current working directory to clipboard in OS X
I frequently find myself in need of copying the path to my current working directory. Under OS X, you can pipe the output of any command to the clipboard using the pbcopy and pbpaste (see man pbcopy
). Easy enough with pwd | pbcopy
, but you end up with a newline. No problem, tr
to the rescue.
pwd | tr -d '\n' | pbcopy
^[1] ^[2] ^[3] ^[4]
pwd
prints the working directory; pwd outputs a newline, which isn’t very handy if you need to paste this elsewhere, and especially unhelpful in a shelltr
is short for translate characters (think find/replace); normally it takes two arguments, but passing the-d
flag deletes the string passed as an argument from the stdin\n
is the escape sequence for newlines; the character we want to get rid ofpbcopy
is the OS X bash clipboard tool; see also: xclip or xsel for X-Windows environments
Use man <command>
for more details about any of the above.
I’ve added the following as a bash alias so I can call this one quickly:
alias cpwd="pwd | tr -d '\n' | pbcopy"
Easy!
Comments are closed.