2

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.

  • 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. – Xen2050 Apr 4 '16 at 13:59
  • 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. – Black Apr 4 '16 at 14:00
  • 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 – Xen2050 Apr 4 '16 at 14:05
6

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

| improve this answer | |
  • Thank you. It is way more complicated then i expected though. – Black Apr 4 '16 at 11:07
  • 1
    Try this should work. Help me with the feedback as well after testing. – manjesh23 Apr 4 '16 at 11:08
  • 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. – Black Apr 4 '16 at 11:21
  • Got you, let me check on this and will keep you posted. – manjesh23 Apr 4 '16 at 11:30
0

In case you decide to put the file path on the system clipboard, you might use this in your ~/.bashrc:

yankpath() {
  filepath=$(realpath "$1")
  # We use the pipe to put the file name on the clipboard.
  # If we did "xclip -selection clipboard $filepath", the
  # contents of the file would be on the clipboard.
  # -rmlastnl removes the ending newline from the file path.
  echo $filepath | xclip -rmlastnl -selection clipboard
}

Then, you can yankpath ./a_file and the entire file path of a_file will be on your X system clipboard.

| improve this answer | |
  • To prevent echo from appending EOL characters to the echoed content use "echo -n 'blah'" – oᴉɹǝɥɔ Aug 16 '19 at 18:14
-1

This works in mac terminal and linux on Digital Ocean.

pbcopy < ~/.ssh/id_rsa.pub

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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