Linux: xclip,pbcopy,xsel用法 terminal 复制粘帖 (mac , ubuntu)

http://justcoding.iteye.com/blog/1829963linux

 

1. Windows下ubuntu

 

使用系统自带的clip命令。
# 位于C:\Windows\system32\clip.exebash

 

示例:app

C代码   收藏代码
  1. echo Hello | clip  
  2. # 将字符串Hello放入Windows剪贴板  
  3.    
  4. dir | clip  
  5. # 将dir命令输出(当前目录列表)放入Windows剪贴板  
  6.    
  7. clip < README.TXT    
  8. # 将readme.txt的文本放入Windows剪贴板  
  9.    
  10. echo | clip  
  11. # 将一个空行放入Windows剪贴板,即清空Windows剪贴板  

 

2. Ubuntu下ssh

 

ubuntu下的用户能够只用apt-get来安装:编辑器

C代码   收藏代码
  1. sudo apt-get install xclip  
 
其余发行版的用户能够选择本身的安装方式,也能够用源码编译安装,xclip项目的主页是: http://sourceforge.net/projects/xclip/

xclip能够将内容输出到‘X’的剪切板中,好比:oop

C代码   收藏代码
  1. echo "Hello, world" | xclip  

 

执行这个命令后你就能够用鼠标中键来在X程序中将内容粘贴出来。可是更多的时候,咱们须要不单单把内容输出到‘X’的剪切板中,而是但愿能够在 GUI程序 中用ctrl + v也能够粘贴(好比,输出到gnome的剪切板中),下面这段命令就可让你将内容输出到gnome的剪切板中:this

C代码   收藏代码
  1. echo "Hello, world" | xclip -selection clipboard  

 

再在一个GUI程序中按下ctrl + v,看下是否是粘贴上去了呢?顺着这个命令,我也从新写了一下ifconfig,让它在执行后输入内容到终端的同时,也将ip地址输出到剪切板中,由于一般状况下,查看ifconfig就是为了获取机器的ip地址:spa

C代码   收藏代码
  1. alias ifconfig='/sbin/ifconfig && echo `/sbin/ifconfig | sed -n 2p | awk "{ print \\$2 }" | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"` | xclip -selection clipboard'  
 

或者.net

C代码   收藏代码
  1. xclip -sel clip < file   

 

此时你就能够在网页等编辑框CTRL+V了。

 

项目主页:http://sourceforge.net/projects/xclip/
命令man page: http://linux.die.net/man/1/xclip

 

-i, -in read text into X selection from standard input or files (default) -o, -out prints the selection to standard out (generally for piping to a file or program) -f, -filter when xclip is invoked in the in mode with output level set to silent (the defaults), the filter option will cause xclip to print the text piped to standard in back to standard out unmodified -l, -loops number of X selection requests (pastes into X applications) to wait for before exiting, with a value of 0 (default) causing xclip to wait for an unlimited number of requests until another application (possibly another invocation of xclip) takes ownership of the selection -d, -display X display to use (e.g. "localhost:0"), xclip defaults to the value in $ DISPLAY if this option is omitted
 
3. Linux下

使用xsel命令。

 

示例:

C代码   收藏代码
  1. cat README.TXT | xsel  
  2. cat README.TXT | xsel -b # 若有问题能够试试-b选项  
  3. xsel < README.TXT  
  4. # 将readme.txt的文本放入剪贴板  
  5.    
  6. xsel -c  
  7. # 清空剪贴板  
 

4. Mac下

 

使用pbcopy命令。 # 对应有个pbpaste命令。

 

示例:

C代码   收藏代码
  1. echo 'Hello World!' | pbcopy  
  2. # 将字符串Hello World放入剪贴板  
 
C代码   收藏代码
  1. cat myFile.txt | pbcopy  
 
C代码   收藏代码
  1. pbpaste > file.txt  
 
要复制结果又想看到命令的输出

命令的结果输出时,若是给复制命令(即上面提到的命令clip、xsel、pbcopy)那么命令输出就看不到了。若是你想先看到命令的输出,能够下面这么作。

C代码   收藏代码
  1. $ echo 'Hello World!' | tee tmp.file.txt  
  2. Hello World!  
  3. $ xsel < tmp.file.txt  
  4. $ rm tmp.file.txt  
 
即先使用 tee命令把输出输到控制台和一个文件中。命令执行完成后,再把输出的内容放到剪贴板中。
 
复制SSH的公有KEY

使用下面的命令:

C代码   收藏代码
  1. $ pbcopy < ~/.ssh/id_rsa.pub  

 

注:不一样系统使用不一样的复制命令。避免用文本编辑器打开这个文件、选中文本、CTRL + C这样繁琐操做。