linux command line copy result to clipboard code example

Example 1: bash copy contents of file to clipboard

# Basic syntax using pbcopy:
pbcopy < /path/to/file

# Paste with:
pbpaste > /path/to/output_file

# Note, you can filter what you paste with grep, e.g.:
pbpaste | grep 'useful text' > /path/to/output_file

Example 2: copy cat command output to clipboard

# This can either be done with xclip or an npm package called clipboard-cli

# With xclip, you would need to sudo install xclip, then use it as so
cat file.txt | xclip

# To paste using sclip
xclip -o

# With the clipboard-cli npm package

# first install globally
npm install --global clipboard-cli

# Then it can be used as follows
cat file.txt | clipboard-cli

# To paste
clipboard