How to copy many files to clipboard from the command line?

Suppose you have different files in a directory: one.txt, two.jpg, three.pdf, four.jpg etc. You want to copy them all to clipboard from the command line. Something like copyclipboard *. How do you do it?

Based on this answer, which solves for a particular case, but has the filetype hardcoded in the script. I wonder if there is a more general solution.

Asked By: Strapakowsky

||

Is this to speed up some workflow you have or for some script?

What I usually do is that when I enter the folder with the files I want to copy I run
nautilus . to get the file browser. The workflow problem I see with copying straight from terminal is that you still need to browse to the folder where you want to paste the files later on.

Please comment if I’m missing something here.

Answered By: CHolmstedt

In fact, the command I posted in the original question was applicable to a wide range of situations:

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

the only requirement is for the first command (find in this case) to generate a list of absolute filenames to files which need to be copied. If you check man find, you’ll see that you can modify the command to find files created later than particular date, larger than particular size, having particular permissions, matching a particular regexp etc.

If you want to have a short-cut command for a particular use case – e.q. copying files from the current directory and below matching a particular mask, you can write a short script:

#!/bin/sh
find "$PWD" -name "$1" | xclip -i -selection clipboard -t text/uri-list

and call it with

cb "*.txt"

(where cb is the name of the script)

Answered By: Sergey

Save this as a script cpfiles

#!/bin/bash
{
    for i in "$@"; do
        echo -en "file://$(realpath ${i})n"
    done
} | xclip -i -selection clipboard -rmlastnl -t text/uri-list

Use it

cpfiles '/path/to/file1/' 'path/to/file2'

Then paste Ctrl+v in file manager

Answered By: superqwerty
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.