1

Is there a way to copy a file to the clipboard from the command line so that I can paste it into a web page's upload area using Ctrl+V? I’m looking for a solution that mimics the behavior of file managers like Dolphin, Nautilus, or Nemo when I press Ctrl+C on a file, but executed from the terminal.

In GUI file managers, pressing Ctrl+C on a file copies it as a "file object", and pasting with Ctrl+V into a web page upload field directly uploads the file.

I tried replicating this GUI behavior with xclip by copying the absolute path with a file:// prefix:

echo -n "file:///path/to/file.txt" | xclip -selection clipboard

However, this approach doesn’t work for file upload in browsers, it only pastes the path as text. How can I mimic the Dolphin, Nemo and Nautilus clipboard behavior when I copy files from them? But using the command line instead?


Example

Let's say I have a file called file.txt in a folder and I'm using Dolphin (it works the same for Nemo or Nautilus)

enter image description here

This file content is:

# Hello World File

This is a file with a content, it has any number of lines.

Now let's say I select this file and press Ctrl+C in it, if I try to paste it in a text editor I'll see

file:///home/Dados/question/file.txt

Well, even though it looks like it's just copying the absolute path of the file, it's actually doing something more. Let's say I open the ChatGPT prompt and press Ctrl+V in it:

enter image description here

It uploads it there as a file, not the absolute path of the file. That's what I'd like to do... However, using xclip or xsel with:

echo -n "file:///path/to/file.txt" | xclip -selection clipboard

or

printf '/path/to/file.txt' | xclip -sel c

These commands don't work as I expect, if I use them and press Ctrl+V on ChatGPT it'll just paste the absolute path of the file:

enter image description here

This behavior is different from the behavior of Dolphin, Nemo and Nautilus when I copy files with them... My main goal is to create a script that will get the clipboard content, create a temporary file, and then save it as a file on the clipboard, something like:

file_content="$(xclip -out -selection clipboard)"
file="$(mktemp)"
echo "$file_content" > "$file"
command-that-saves-file-as-object "$file"

Doing this allows me to attach this script to a shortcut, then I can quickly upload content to ChatGPT as files. I could manually open a file, paste the text to it, and then upload it there, but I want to optimize this process, and for that I need a way of doing what Dolphin, Nautilus and Nemo do when I copy a file from them.

raylight
  • 533

0 Answers0