Skip to content

SeriousPassenger/copyfile-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

copyfile

copyfile is a small Bash CLI that copies the contents of a file to the system clipboard on Linux desktops and WSL.

It is safe by default:

  • Running ./copyfile with no arguments only checks your environment and prints help.
  • It does not install dependencies unless you explicitly run ./copyfile --install-deps.
  • It does not install itself systemwide unless you explicitly run ./copyfile --install-systemwide.

Supported environments

copyfile chooses the clipboard backend from the active session type, not just the desktop environment name. This matters because GNOME, KDE Plasma, and other desktops can run under either Wayland or X11.

Supported desktop/session types

Environment Backend used Required command Package usually needed Support status
Wayland desktops/compositors Wayland clipboard wl-copy wl-clipboard Supported
X11/Xorg desktops X11 clipboard xclip, fallback xsel xclip or xsel Supported
WSL/WSL2 Windows clipboard clip.exe none Supported when Windows interop is enabled

Common supported desktop environments include:

  • GNOME on Wayland or X11
  • KDE Plasma on Wayland or X11
  • Xfce on X11
  • Cinnamon on X11
  • MATE on X11
  • LXQt/LXDE on X11
  • Sway, Hyprland, River, and other Wayland compositors using wl-clipboard

Not guaranteed:

  • Headless TTY-only systems
  • Remote SSH sessions without a forwarded graphical clipboard
  • Containers without access to the host display server
  • macOS
  • BSD systems
  • NixOS, Guix System, and immutable/atomic Linux variants unless the required backend command is already installed or installed by your normal system method
  • Nonstandard clipboard managers or restricted desktop sessions

Supported distributions for automatic dependency installation

--install-deps has built-in package-manager support for the following Linux families.

Distribution family Package manager detected Wayland package X11 package Support status
Debian, Ubuntu, Linux Mint, Pop!_OS, Kali apt-get wl-clipboard xclip Supported
Fedora dnf wl-clipboard xclip Supported
RHEL, CentOS Stream, Rocky Linux, AlmaLinux dnf/yum wl-clipboard where available xclip Supported, repository availability may vary
Arch Linux, Manjaro, EndeavourOS pacman wl-clipboard xclip Supported
openSUSE Leap/Tumbleweed zypper wl-clipboard xclip Supported
Alpine Linux apk wl-clipboard xclip Supported
Void Linux xbps-install wl-clipboard xclip Supported
Gentoo emerge gui-apps/wl-clipboard x11-misc/xclip Supported
Solus eopkg wl-clipboard xclip Supported
WSL/WSL2 none not needed not needed Supported through clip.exe

Any distribution not listed above is not guaranteed. The script may still work if one of these commands is already available in PATH:

  • wl-copy
  • xclip
  • xsel
  • clip.exe

Usage

Check your system and print help:

./copyfile

Copy a file:

./copyfile notes.txt

Copy stdin:

printf 'hello\n' | ./copyfile -

Install the required clipboard dependency for the current session:

./copyfile --install-deps

Install copyfile systemwide:

./copyfile --install-systemwide

After systemwide installation:

copyfile notes.txt

First run behavior

The first run is intentionally non-invasive:

./copyfile

It reports:

  • distribution
  • desktop environment
  • session type: wayland, x11, wsl, or unknown
  • detected package manager
  • available or missing clipboard backend
  • the dependency command to run when needed
  • the systemwide installation command

If a dependency is missing, it tells you to run:

./copyfile --install-deps

Installation model

Dependency installation

--install-deps installs only the clipboard backend needed for the detected session:

  • Wayland: installs wl-clipboard, which provides wl-copy
  • X11: installs xclip
  • WSL: uses Windows clip.exe; no Linux package is installed

Package installation uses sudo when needed. If sudo is not available, it tries doas.

Systemwide installation

--install-systemwide installs the current script to:

/usr/local/bin/copyfile

This makes it callable from anywhere as:

copyfile FILE

The target path can be overridden with COPYFILE_INSTALL_PATH:

COPYFILE_INSTALL_PATH="$HOME/.local/bin/copyfile" ./copyfile --install-systemwide

Exit behavior

copyfile is quiet on success and returns exit code 0.

It prints errors to stderr and returns a nonzero exit code when:

  • the file does not exist
  • the input is a directory
  • the file is not readable
  • no supported clipboard backend is available
  • dependency installation fails
  • systemwide installation fails

Examples

Copy a public SSH key:

copyfile ~/.ssh/id_ed25519.pub

Copy a script:

copyfile ManageKVM.sh

Copy command output:

git diff | copyfile -

Copy a file whose name starts with a dash:

copyfile -- -filename.txt

Uninstall

Remove the systemwide command:

sudo rm /usr/local/bin/copyfile

This does not remove clipboard dependencies such as wl-clipboard, xclip, or xsel.

About

Portable CLI for copying files or stdin to the clipboard on Linux and WSL, with desktop/session detection and optional dependency installation.

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages