Copying files from command line to clipboard

7,428

Solution 1

Yes, basically, you'd need to offer the CLIPBOARD selection either as

  • text/uri-list with the content being

    /path/to/file1
    /path/to/file2
    
  • application/x-kde-cutselection or x-special/gnome-copied-files with content copy\nfile://$path1\nfile://$path2\0 or cut\nfile://$path1\nfile://$path2...\0

With xclip you can achieve this with something like

find "$PWD" -name "*.pdf"| xclip -i -selection clipboard -t text/uri-list

I've also found this loliclip command that looked promising, but though I could retrieve the values, I wasn't able to store them and have them retrieved from loliclip by pcmanfm successfully.

You also should be able to implement it in a few lines of perl-tk.

Solution 2

why not just make find do it for you?

find ${PWD} -name "*.txt" -exec cp {} /full/path \; && gnome-open /full/path &

EDIT: from what I understand from man xclip: it handles text only, not files or directories.

Solution 3

Based on these answers I wrote a filetoclip Python script, which copies to the clipboard the files specified on the command line.

Essentially, all that's needed is to absolutize them and percent-encode "strange" characters before copying to the clipboard. The actual clipboard operation is delegated to xclip, as working with clipboard straight from Python is more painful than expected.

#!/usr/bin/env python3
import os.path
import sys
from urllib.parse import quote
from subprocess import run
out = [os.fsencode('file://' + quote(os.path.abspath(x))) for x in sys.argv[1:]]
run(['xclip', '-i', '-selection', 'clipboard', '-t', 'text/uri-list'],
    input=b'\n'.join(out), check=True)
Share:
7,428

Related videos on Youtube

Faisal Khan Samrat
Author by

Faisal Khan Samrat

Updated on September 18, 2022

Comments

  • Faisal Khan Samrat
    Faisal Khan Samrat over 1 year

    In a GUI file manager it is possible to select a few files, press Ctrl-C (which supposedly copies come info about the files to clipboard), then navigate to another folder and press Ctrl-V, which will then copy the files into that directory.

    As an experiment, after copying files in the file manager, it is possible to switch to a text editor - pressing Ctrl-V there pastes a list of absolute filenames. The reverse process (copying a list of files from a text editor and pasting them to a file manager) does not work, which is supposedly due to different target atoms

    The goal of the exercise is to be able to copy some files from command line, for example

    find ${PWD} -name "*.txt" | xclip <magic parameters>
    

    then switch to a file manager and copy them all to a directory using File->Paste.

    So, the question is: What parameters of xclip (or other program) do I need to specify so file manager recognizes the selection as a list of files and enables its Paste menu item?

    Alternatively, is there a low-level tool which would allow to inspect the contents of X selection and see what data it currently contains?

  • Faisal Khan Samrat
    Faisal Khan Samrat over 11 years
    Mostly because it's an experiment, although I think the "copy files to clipboard now, decide where you want to paste them later" would be a useful way to do things.
  • Faisal Khan Samrat
    Faisal Khan Samrat over 11 years
    Ok, after some tinkering the text/uri-list variant worked for me - I'm able to copy stuff from command line and paste files into Dolphin file manager. It doesn't even require file:// prefixes, which make the command to work fine with find. I've edited your question to add the command which works for me in KDE - can anybody test it in Gnome or elsewhere?
  • Stéphane Chazelas
    Stéphane Chazelas over 11 years
    Thanks for the edit. It looks like the file:// is not necessary with pcmanfm either (though pcmanfm does include it itself). I suppose some special characters (at the very least LF) should be URI-encoded though.
  • Stéphane Chazelas
    Stéphane Chazelas over 11 years
    Yes. I can confirm you need to use file:///new%0Aline for files that contain a newline characters, though any other character (including non-ASCII in any encoding) seems to be fine (with pcmanfm at least)
  • Admin
    Admin almost 2 years
    Dolphin now seems to require the file:// scheme prefix