126

What is the command line equivalent to pressing CTRL+C over a file in the file manager so that the file (not the filename) is copied to the clipboard?

A situation where this can be useful and fast, for example, is when you want to copy to the clipboard a file from the directory you are in the terminal to quickly paste the file in the directory you are in the file manager. There are others.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
Strapakowsky
  • 11,914
  • This really doesn't look like a duplicate to me. One answer is about general copy paste generally, and this one is about copy a file specific subject – Ulysse BN Aug 25 '17 at 19:07
  • 1
    How about a when a complete non-GUI Ubuntu is being used through ssh from macOS Terminal app or analogue? – Ricardo Dec 18 '20 at 20:42

4 Answers4

129

When you press Ctrl-C over a file in the file manager, the file's contents IS NOT copied to the clipboard. A simple test: select a file in file manager, press Ctrl-C, open a text editor, press Ctrl-V. The result is not file's contents but its full path.

In reality the situation is a bit more complicated because you can't do the opposite - copy a list of filenames from a text editor and paste them into file manager.

To copy some data from command line to X11 clipboard you can use xclip command, which can be installed with

sudo apt-get install xclip

to copy contents of a file or output of some command to clipboard use

cat ./myfile.txt|xclip -i

the text can be then pasted somewhere using middle mouse button (this is called "primary selection buffer").

If you want to copy data to the "clipboard" selection, so it can be pasted into an application with Ctrl-V, you can do

cat ./myfile.txt|xclip -i -selection clipboard

To be able to copy files from the command line and paste them in a file manager, you need to specify a correct "target atom" so the file manager recognizes the data in the clipboard, and also provide the data in correct format - luckily, in case of copying files in a file manager it's just a list of absolute filenames, each on a new line, something which is easy to generate using find command:

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

(at least this works for me in KDE). Now you can wrap into a small script which you can call, say, cb:

#!/bin/sh
xclip -i -selection clipboard -t text/uri-list

then you put it in ~/bin, set executable bit on it and use it like this:

find ${PWD} -name "*.txt"| cb

Nice, isn't it?

Sergey
  • 43,665
  • 1
    Nice, but only works for text and it's not the file really, just the text. Suppose you have a jpg file? – Strapakowsky Nov 01 '12 at 09:33
  • Yes, I knew this effect that if you copy a file from the file manager if you paste it in another folder you paste the file, but if you paste in a text editor you get the file path. – Strapakowsky Nov 01 '12 at 09:34
  • In case of binary files (jpg etc.) everything is much more complicated. Here I asked a question inspired by yours - http://unix.stackexchange.com/questions/53503/copying-files-from-command-line-to-clipboard - have a read about "target atoms" and stuff – Sergey Nov 01 '12 at 09:48
  • 1
    I heard that xclip also supports file copying with xclip-copyfile and xclip-pastefile. I haven't really used it though, but it might be a solution. – Gladen Nov 01 '12 at 09:50
  • Wow, @Gladen, I think you need to post it as a separate answer. Although it does work when using xclip-copyfile and then xclip-pastefile, but doesn't seem to work with Ubuntu file manager... – Sergey Nov 01 '12 at 09:54
  • Haha, alright will do.. :P Maybe other people might find it usefull too. Hm, I don't really know if that's possible with xclip then since it's mainly a command line tool.. I think Ubuntu's file manager and xclip store the copied file in a different way or something.. – Gladen Nov 01 '12 at 10:02
  • Doesn't work. It must copy the file not to clipboard but to some temporary folder specific to xclip, and the paste command moves it from there. – Strapakowsky Nov 01 '12 at 10:20
  • @strapakowsky: I've edited the answer to show how you can copy files from command line and paste them to the file manager. – Sergey Nov 01 '12 at 22:41
  • @Sergey That's excellent, and I didn't know about this option. I posted a separate question to see if there's a general solution: http://askubuntu.com/questions/211250/how-to-copy-many-files-to-clipboard-from-the-command-line – Strapakowsky Nov 03 '12 at 03:54
  • Once you get a command like one of the above to do what you want, you can associate it with a keyboard shortcut using a utility such as AutoKey. Then, it will be just like your shell had that feature built-in. – Joe Nov 07 '12 at 19:22
  • Minor comment: -i seems to be default behaviour, so could be omitted from your examples. – Duncan Jones Sep 27 '17 at 09:36
  • I was unable to replicate this trying to copy a pdf file. In . I had test.pdf so I tried: find ${PWD} -name "test.pdf"| xclip -i -selection clipboard -t text/uri-list and executed "strg+v" afterwards. Returned a: "Failed to launch operation: invalid argument" – Natan Aug 06 '22 at 17:04
11

I heard that xclip also supports file copying with xclip-copyfile and xclip-pastefile. I haven't really used it though, but it might be a solution.

Gladen
  • 2,726
  • That just copies the file names, not the contents of the files. Just take a look at the man page examples, i.e. $ man xclip-copyfile – Craig May 08 '17 at 15:19
  • This does work and indeed copies the entire files. But there is no option that allows you to copy to clipboard (X-clipboard is used instead). Files can only be pasted using xclip-pastefile. – Natan Aug 05 '22 at 15:47
6

Mac OS has pbcopy with easier syntax:

pbcopy < ~/.ssh/id_rsa.pub 

or

cat ~/.ssh/id_rsa.pub  | pbcopy

To simulate pbcopy on Ubuntu with xclip (installed via sudo apt install xclip):

alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
0

If you want to copy files in terminal and then be able to paste them in filemanager:
Save this as a script cpfiles

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

Use it

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

Then paste Ctrl+v in file manager

superqwerty
  • 45
  • 1
  • 1
  • 7