Linux - copy/cut file into clipboard

15,386

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?

Source from askubuntu

Share:
15,386

Related videos on Youtube

Black
Author by

Black

Full-Stack Webdeveloper, Magento & more.

Updated on September 18, 2022

Comments

  • Black
    Black almost 2 years

    I was wondering if it is possible to copy or cut a file into the clipboard and then paste it to another directory later on. I did a quick research and only found information on how to copy the content of a file into the clipboard, but not the file itself.

    • Xen2050
      Xen2050 about 8 years
      This is how most file managers work, right? Cut/copy a file from one folder, navigate to another folder and paste, it moves or copies the file. It only stores the filename/path in the "real" clipboard. What program are you using? Or do you want to delete/move the file first, and then pick a destination for it second? That sounds like using a temp folder (like /tmp) is required, cut & paste to temp first, then cut & paste from temp to real destination.
    • Black
      Black about 8 years
      What do you mean, what program am i using? I just use the console and try to copy or cut a file and then cd to another directory and paste it there.
    • Xen2050
      Xen2050 about 8 years
      You mean in bash, in a terminal? Like in ubuntu, searching for & running "Terminal"? Bash can do cut & yank on the command line, with ctrl-K or W & others, and ctrl-Y, but that's a little different
  • Black
    Black about 8 years
    Thank you. It is way more complicated then i expected though.
  • manjesh23
    manjesh23 about 8 years
    Try this should work. Help me with the feedback as well after testing.
  • Black
    Black about 8 years
    I tried it and it works. But i needed to put the cb script not to the home directory ~/bin, i had to put it to the /bin folder which is in the root directory. It seems like your solution is only for the purpose of copying the content of a file, but i try to copy/cut the file itself so i can copy it to another location. Just like i would use ctrl+c or ctrl+x and then paste it to another location, but all in the console. Excuse my english.
  • manjesh23
    manjesh23 about 8 years
    Got you, let me check on this and will keep you posted.
  • oᴉɹǝɥɔ
    oᴉɹǝɥɔ almost 5 years
    To prevent echo from appending EOL characters to the echoed content use "echo -n 'blah'"
  • stegetsj
    stegetsj over 3 years
    It is not work as I want. I want copy file as drag-to-drop. When I copy from file-menager to textarea with only text then, yeah, I get the path to file. But if textarea is with drag-to-drop then nothing happens or it sends path to file. But from nemo I send file not text.
  • efreed
    efreed over 2 years
    pbcopy is exactly what I was looking for, I was wanting to pipe a command's output to the clipboard. So a different answer to the OP's question could be: cat filepath | pbcopy
  • Flavio
    Flavio over 2 years
    @efreed glad to help.