Skip to content

Instantly share code, notes, and snippets.

@christophetd
Last active May 15, 2020 14:52
Show Gist options
  • Save christophetd/63f8986dcd367c758dac to your computer and use it in GitHub Desktop.
Save christophetd/63f8986dcd367c758dac to your computer and use it in GitHub Desktop.
Command line clipboard

Simple command line clipboard using xclip

Installation

# Install xclip (if not already installed)
if [ -z `which xclip` ]; then sudo apt-get install xclip; fi

# Download script and make it executable
sudo wget -O /usr/bin/clipboard https://gist.githubusercontent.com/christophetd/63f8986dcd367c758dac/raw/9017ce92c98fb80c01c73938d583e84416d026c4/clipboard
sudo chmod +x /usr/bin/clipboard

Usage

some-command | clipboard [-p]

Executes 'some-command', copies its output to the clipboard and prints it to STDOUT if the -p option is specified

Examples :

  • ls | clipboard -p Prints the content of the current directory and copies it to the clipboard

  • cat /var/log/apache2/access.log | clipboard Copies the content of the apache access log in the clipboard

  • cat somefile.txt | clipboard -p | grep banana | wc -l (a bit more advanced) Copies the content of somefile.txt in the clipboard and prints the number of lines the word 'banana' appears in

#!/bin/bash
if [ -z `which xclip` ]; then
echo "You must instal xclip (sudo apt-get install xclip) to use this script" 1>&2
exit 1
fi
if [ "$1" = "-p" ]; then
exec 3>&1
cat | tee /dev/fd/3 | xclip -sel clip
else
cat | xclip -sel clip
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment