Skip to content

Instantly share code, notes, and snippets.

@sutnistj
Last active October 14, 2020 07:54
Show Gist options
  • Save sutnistj/08c8df987c69d6484cd5a2ea4c0740ec to your computer and use it in GitHub Desktop.
Save sutnistj/08c8df987c69d6484cd5a2ea4c0740ec to your computer and use it in GitHub Desktop.
Copy file(s) from console into clipboard
#!/bin/bash
# clipboard-files - Copy files from the clipboard in a Linux terminal.
#
# Read more: https://replace.org.ua/topic/12326/
#
# By dot as know as pt
#
# Clipboard interfacing code inspired from:
# https://github.com/larspontoppidan/clipboard-files
# https://redd.it/hs9kv1
#
SCRIPT_REV=2019-10-14
# Is this script being sourced?
if [[ -z $BASH_SOURCE ]] || [[ $(basename ${0#-}) != ${BASH_SOURCE##*/} ]]; then
# Being sourced
sourced=true
EXIT=return
else
# Running directly
sourced=false
EXIT=exit
fi
if [ $# -lt 1 ]; then
syntax_error
$EXIT
fi
xclip -version 1>/dev/null 2>/dev/null
result=$?
if [ $result != 0 ]; then
echo "xclip doesn't seem to be installed, exiting"
$EXIT
fi
clipboard=""
count=0
for one_arg in "$@"
do
if [[ ${one_arg} == -* ]]; then
syntax_error
$EXIT
fi
filename=$(realpath "${one_arg}")
if [ -e "${filename}" ]; then
clipboard="${clipboard}\nfile://${filename}"
count=$((count+1))
else
echo "File doesn't exist or error: ${filename}"
fi
done
if [ $count -ge 1 ]; then
echo -en "${clipboard}" | xclip -i -selection clipboard -t text/uri-list
result=$?
if [ $result != 0 ]; then
echo "Error copying to clipboard"
else
echo "${count} item(s) copied to clipboard"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment