Skip to content

Instantly share code, notes, and snippets.

@fsimonis
Last active August 14, 2023 09:49
Show Gist options
  • Save fsimonis/8e6858cec7a387e3d5add43bcef476e9 to your computer and use it in GitHub Desktop.
Save fsimonis/8e6858cec7a387e3d5add43bcef476e9 to your computer and use it in GitHub Desktop.
Copy path to file as URI to xclip clipboard.

xclip-copy-fileuri

Use this tool to copy a path to a file in a terminal if you want to paste it in a GUI.

The tool

  • looksup the absolute path of the file
  • formats a file:// uri
  • passes it to xclip with the target text/uri-list
#!/bin/bash
# Copies the path to a file as a URI to the clipboard.
# This allows pasting the file in GUI applications.
set -e
if [ ! $# -eq 1 ]; then
echo "Usage: $(basename $0) file" >&2
exit 1
fi
FILEPATH=$(realpath "$1")
echo "file:///$FILEPATH" | xclip -i -selection clipboard -t text/uri-list
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment