搜索
首页系统教程LINUX如何在Linux上使用PBCOPY和PBPASTE命令

Since Linux and Mac OS X are Unix-based systems, many commands will work on both platforms. However, some commands are unique and might not be available on both platforms, for example pbcopy and pbpaste commands. These commands are exclusively available only on macOS platform.

Those who have switched from macOS to Linux might feel the absence of this command pair and would likely prefer to continue using them in Linux system. Worry not! If you've moved from macOS to Linux and are missing the pbcopy and pbpaste, I have a solution for you to bring similar functionality to Linux.

In this brief tutorial, I will explain how to replicate the functionalities of pbcopy and pbpaste and use Pbcopy and Pbpaste commands on a Linux system.

Table of Contents

What are pbcopy and pbpaste Commands?

Pbcopy and Pbpaste are two command-line utilities on macOS that allow you to copy and paste text to and from the clipboard. They are similar to the familiar keyboard shortcuts ⌘+C and ⌘+V, but they can be used from within the terminal.

The pbcopy command will copy the standard input into clipboard. You can then paste the clipboard contents using pbpaste command wherever you want.

Here are some examples of how to use pbcopy and pbpaste:

Copy the text "Hello world!" to the clipboard:

pbcopy echo "Welcome to OSTechNix blog!"

Paste the contents of the clipboard into a new file:

pbpaste > mynewfile.txt

Copy the output of the command ls to the clipboard:

ls | pbcopy

While pbcopy and pbpaste are specifically available on macOS systems, there are a number of Linux alternatives to pbcopy and pbpaste, such as xclip and xsel.

Heads Up: The symbol ⌘, also known as the looped square, is the symbol for the command key on Apple keyboards.

What are xclip and xsel Commands?

Xclip is a command line interface to X selections i.e. Clipboard. Xclip reads the data from one or more files and makes the data available as an X selection for pasting the data into X applications.

Xsel is also a command line X11 selection and clipboard manipulation tool. It is used to access X clipboard and selection buffers in Linux and Unix-like operating systems.

For more details on xclip and xsel usage in Linux, please refer the following guide.

How To Access Clipboard Contents Using Xclip and Xsel In Linux

Using xclip or xsel programs, we can easily imitate the functionality of pbcopy and pbpaste commands in Linux.

Install xclip and xsel in Linux

Both xclip and xsel packages available in the default repositories of most Linux distributions. Please note that you don't have to install both utilities. Just install any one of the above utilities.

To install them on Arch Linux and its derivatives, run:

$ sudo pacman -S xclip xsel

On Fedora:

$ sudo dnf install xclip xsel

On Debian, Ubuntu, Linux Mint:

$ sudo apt install xclip xsel

Once installed, you need to create aliases for pbcopy and pbpaste commands.

Create Aliases for pbcopy and pbpaste

We can replicate the functionality of pbcopy and pbpaste commands using xclip and/or xsel commands via shell aliasing. Let us see how to do that!

1. Edit your ~/.bashrc file:

$ vi ~/.bashrc

2. If you want to use xclip, paste the following lines:

alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'

3. If you want to use xsel, paste the following lines in your ~/.bashrc file.

alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'

4. After adding the above lines, press ESC and :wq to save and close the file.

5. Next, run the following command to update the changes in ~/.bashrc file.

$ source ~/.bashrc

6. The ZSH users paste the above lines in ~/.zshrc file and update the changes using command:

$ source ~/.zshrc

Use Pbcopy and Pbpaste Commands on Linux

As mentioned already. the pbcopy command will copy the text from stdin into clipboard buffer. For example, have a look at the following example.

$ echo "Welcome To OSTechNix!" | pbcopy

The above command will copy the text "Welcome To OSTechNix" into clipboard. You can access this content later and paste them anywhere you want using pbpaste command like below.

$ echo `pbpaste`
Welcome To OSTechNix!

如何在Linux上使用PBCOPY和PBPASTE命令

Here are some other use cases.

I have a file named file.txt with the following contents.

<strong>$ cat file.txt</strong> 
Welcome To OSTechNix!

You can directly copy the contents of a file into a clipboard as shown below.

$ pbcopy <p>Now, the contents of the file is available in the clipboard as long as you updated it with another file's contents.</p><p>To retrieve the contents from clipboard, simply type:</p><pre class="brush:php;toolbar:false"><strong>$ pbpaste</strong> 
Welcome To OSTechNix!

You can also send the output of any Linux command to clip board using pipeline character. Have a look at the following example.

$ ps aux | pbcopy

Now, type "pbpaste" command at any time to display the output of "ps aux" command from the clipboard.

$ pbpaste

如何在Linux上使用PBCOPY和PBPASTE命令

There is much more you can do with Pbcopy and Pbpaste commands. I hope you now got the basic idea about these commands.

Frequently Asked Questions

FAQ: Using Xclip and Xsel in Linux as Alternatives to pbcopy and pbpaste.

Q: What are pbcopy and pbpaste, and why are they not available on Linux?

A: pbcopy and pbpaste are commands on macOS for copying and pasting text via the command line. They are unavailable on Linux. These commands are specific to macOS.

Q: What are Xclip and Xsel?

A: Xclip and Xsel are Linux command-line tools that replicate the functionality of pbcopy and pbpaste. They enable efficient copying and pasting of text between the command line and clipboard.

Q: How do I install Xclip and Xsel on my Linux system?

A: You can install Xclip and Xsel using your package manager. For example, on Debian-based systems like Ubuntu, you can use sudo apt-get install xclip xsel.

Q: How do I copy text to the clipboard using Xclip?

A: To copy text from a file to the clipboard, use: cat file.txt | xclip -selection clipboard.

Q: How do I paste text from the clipboard using Xclip?

A: To paste text from the clipboard to the terminal, use: xclip -selection clipboard -o.

Q: Can I use Xclip and Xsel on macOS?

A: Yes, you can, but they are most commonly used on Linux systems. macOS already provides the native pbcopy and pbpaste commands.

Q: Are the commands for Xclip and Xsel the same as pbcopy and pbpaste?

A: No, the commands are different, but they serve similar purposes. Xclip and Xsel have their own syntax for copying and pasting text.

Q: Can I use Xclip and Xsel for tasks beyond simple copying and pasting?

A: Yes, you can use them in scripting and automation for more complex tasks involving clipboard content.

Q: Are there any other alternatives to Xclip and Xsel for Linux?

A: While Xclip and Xsel are popular choices, you might also consider other tools like wl-clipboard and clipman. They are exclusively developed for Wayland systems.

Q: Do Xclip and Xsel work across different Linux distributions?

A: Yes, Xclip and Xsel are widely supported and work across various Linux distributions.

Conclusion

To sum it up, even though pbcopy and pbpaste commands are handy for controlling clipboard on Mac computers, Linux users can accomplish similar tasks using tools like Xclip and Xsel. These tools let you handle clipboard content smoothly in Linux, making it easy to work with text. So, while the names of the commands may change, the main idea of improving how you work with text stays the same whether you're using macOS or Linux.

Related Read:

  • How To Manage Clipboard From Command Line In Linux
  • How To Manage Clipboard Contents With CopyQ In Linux
  • Access Clipboard Contents Using Xclip and Xsel In Linux
  • Copy File Contents Into Clipboard Without Displaying Them In Linux

以上是如何在Linux上使用PBCOPY和PBPASTE命令的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
仓库:用于轻松处理Flatpak应用的GUI仓库:用于轻松处理Flatpak应用的GUIMay 09, 2025 am 11:30 AM

用于轻松Flatpak管理的GUI:介绍仓库 仅使用命令行管理越来越多的FlatPak应用程序集合可能会很麻烦。 输入仓库,这是一种用户友好的图形接口,旨在简化flatpak a

8个强大的Linux命令来识别硬盘驱动器瓶颈8个强大的Linux命令来识别硬盘驱动器瓶颈May 09, 2025 am 11:03 AM

本文为识别和解决Linux系统中的硬盘驱动器瓶颈提供了综合指南。 经验丰富的服务器管理员会发现这特别有用。缓慢的磁盘操作会严重影响应用程序性能,

Linux用户的4个最佳QR码生成器Linux用户的4个最佳QR码生成器May 09, 2025 am 10:27 AM

Linux系统下的高效二维码生成工具 在当今数字世界中,二维码已成为快速便捷地共享信息的一种方式,从URL、文本、联系人、Wi-Fi凭证,甚至支付信息,二维码都简化了数据访问。 Linux用户可以使用多种工具高效地创建二维码。让我们来看看一些可在Linux系统上直接使用的流行二维码生成器。 QRencode QRencode是一个轻量级的命令行工具,用于在Linux上生成二维码,它以其简单性和效率而广受好评,深受偏爱直接方法的Linux用户的欢迎。 使用QRencode,您可以为URL、

基本操作系统8:用于MACOS和Windows的用户友好型Linux基本操作系统8:用于MACOS和Windows的用户友好型LinuxMay 09, 2025 am 10:19 AM

基本操作系统8 Circe:一种流畅,时尚的Linux体验 基于Ubuntu的Linux发行版,Elementary OS已从简单的主题包演变为一个完整的独立操作系统。 以其用户友好的界面而闻名

每个机器学习工程师的40个Linux命令每个机器学习工程师的40个Linux命令May 09, 2025 am 10:06 AM

掌握Linux对于任何机器学习(ML)工程师至关重要。 它的命令行界面提供了无与伦比的灵活性和控制,简化工作流程并提高了生产力。本文概述了必需的Linux命令,FO解释说

Arch Linux备忘单:初学者的基本命令Arch Linux备忘单:初学者的基本命令May 09, 2025 am 09:54 AM

Arch Linux:初学者的命令行备忘单 Arch Linux提供了无与伦比的控制,但对于新移民来说可能会令人生畏。该备忘单提供了自信管理系统的必要命令。 系统信息和更新 这些com

如何在Linux上安装用于机器学习的Scikit-Learn如何在Linux上安装用于机器学习的Scikit-LearnMay 09, 2025 am 09:53 AM

本指南提供了在Linux Systems上安装和使用Scikit-Learn机器学习库的全面演练。 Scikit-Learn(Sklearn)是一个功能强大的开源python库,为各种机器提供多种工具L

如何在Ubuntu中安装Kali Linux工具如何在Ubuntu中安装Kali Linux工具May 09, 2025 am 09:46 AM

本指南说明了如何利用Docker访问Kali Linux工具,这是Katoolin等过时方法的更安全,更有效的替代方案。 Katoolin不再积极维护,可能会在现代系统上引起兼容性问题。做

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具