pbcopy - macOS
Overview
pbcopy is a command-line utility used to copy text to the macOS clipboard. It is commonly used in scripts, automation workflows, and command chains to move text between applications and the clipboard.
Syntax
pbcopy [-p] [-f] [-v] [text]
Options/Flags
- -p: Paste from the specified file instead of standard input.
- -f: Format the text in the specified file as RTF before copying it to the clipboard.
- -v: Verbose output. Reports on the file being processed and the resulting copy to the clipboard.
Examples
Copy the contents of a file to the clipboard:
pbcopy myfile.txt
Copy formatted RTF text from a file to the clipboard:
pbcopy -f myfile.rtf
Pipe the output of another command to pbcopy:
ls -l | pbcopy
Common Issues
- Error: File not found: Ensure that the specified file exists and is accessible.
- Error: Permission denied: Check if you have sufficient permissions to read the specified file.
- Empty clipboard after using pbcopy: Some applications may clear the clipboard after writing to it. Check if the application you’re using behaves this way.
Integration
Combine with grep: Filter text before copying it to the clipboard.
grep pattern myfile.txt | pbcopy
Use with xargs: Execute a command on each line of text copied to the clipboard.
pbcopy | xargs echo
Related Commands
- pbpaste: Paste text from the clipboard.
- cat: Concatenate files and print on standard output.
- xclip: Cross-platform clipboard utility.