Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 33 down vote accepted

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?

share|improve this answer
    
Nice, but only works for text and it's not the file really, just the text. Suppose you have a jpg file? –  Strapakowsky Nov 1 '12 at 9: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 1 '12 at 9:34
    
In case of binary files (jpg etc.) everything is much more complicated. Here I asked a question inspired by yours - unix.stackexchange.com/questions/53503/… - have a read about "target atoms" and stuff –  Sergey Nov 1 '12 at 9:48
    
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 1 '12 at 9: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 1 '12 at 9:54
show 5 more comments

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.