The “pbcopy” is the command for the Mac Operating system that enables the user to copy the command’s output in the terminal. After that, paste it by utilizing the “pbpaste” command. However, these commands are not available on Linux distributions. This command is a habit for Mac users, and they might want to use them when switching from Mac to Linux.
This post will enlighten how to use the “pbcopy” and “pbpaste” commands in Linux distributions.
- Install pbcopy And pbpaste Commands in Linux
- Use pbcopy And pbpaste Commands in Linux
Install pbcopy And pbpaste Commands in Linux
As mentioned earlier, the “pbcopy” and “pbpaste” commands are not available in Linux. But the user can use these commands by installing the “xclip” and “xsel” utilities and defining the aliases for “pbcopy” and “pbpaste.” The “xclip” and “xsel” utilities copy and paste the content to the clipboard.
The following steps justify the above discussion.
Step 1: Install xclip & xsel Utilities
Open the terminal and install the “xclip” and “xsel” utilities using the below-given command based on the Linux distro:
$ sudo apt install xclip xsel #For Ubuntu/Debian $ sudo dnf xclip xsel #Fedora $ sudo yum xclip xsel #CentOS/RHEL $ sudo pacman xclip xsel #For Arch
The “xclip” and “xsel” are installed in the system.
Step 2: Add Aliases “.bashrc” File
Now, open the “.bashrc” file with the “nano” editor and define the aliases for “pbcopy” and “pbpaste” as shown:
$ nano .bashrc #Opens the file with nano editor alias pbcopy='xclip -selection clipboard' alias pbpaste='xclip -selection clipboard -o'
Save the file by pressing “Ctrl+O” and exit from the file using “Ctrl+X”.
Step 3: Execute the “.bashrc” File
To apply the changes in the “.bashrc” file, apply the source command to re-execute it:
$ source .bashrc
The “.bashrc” file is executed.
Use pbcopy and pbpaste Commands on Linux
To use pbcopy for copying any command output; the following syntax is carried out:
$ [command] | pbcopy
- Write any command in the “Command” Section
- Write “pbcopy” separated by the pipe (|) command
Example 1: Copy File Content
To verify the added aliases are working let’s copy the content of the “file.txt” in the clipboard using “pbcopy”:
$ cat file.txt | pbcopy
The content of “file. txt” is copied.
Execute the “pbpaste” command to paste in the terminal:
The copied file content is pasted as shown in the above image.
Example 2: Copy Command Output
Likewise, the user can copy any command output as in the following command, the output of the “pwd” command is copied:
$ pwd | pbcopy
The command output is copied.
To paste the command output, run the “pbpaste” command in the terminal:
The command output (/home/itslinuxfoss) of the “pwd” command is displayed.
To use the “pbcopy” and “pbpaste” commands, install the “xclip” and “xsel” and then define the aliases in the “.bashrc” file. Then utilize the syntax “[command] | pbcopy” to copy the command’s output and paste it using the “pbpaste” command.
This write-up has illuminated the step-step guide to using the “pbcopy” and “pbaste” commands in Linux.