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.
4 Answers
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?
-
-
1Try this should work. Help me with the feedback as well after testing. Apr 4, 2016 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 usectrl+c
orctrl+x
and then paste it to another location, but all in the console. Excuse my english.– BlackApr 4, 2016 at 11:21 -
-
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. Oct 14, 2020 at 4:00
This works in mac terminal and linux on Digital Ocean.
pbcopy < ~/.ssh/id_rsa.pub
-
1pbcopy 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– efreedOct 13, 2021 at 15:28
-
1
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.
-
To prevent echo from appending EOL characters to the echoed content use "echo -n 'blah'"– oᴉɹǝɥɔAug 16, 2019 at 18:14
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
cd
to another directory and paste it there.