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.
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 withto copy contents of a file or output of some command to clipboard use
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
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:(at least this works for me in KDE). Now you can wrap into a small script which you can call, say,
cb
:then you put it in
~/bin
, set executable bit on it and use it like this:Nice, isn't it?
I heard that xclip also supports file copying with
xclip-copyfile
andxclip-pastefile
. I haven't really used it though, but it might be a solution.Mac OS has
pbcopy
with easier syntax:or
To simulate
pbcopy
on Ubuntu withxclip
(installed viasudo apt install xclip
):