- What is the command line equivalent of copying a file to clipboard?
- 4 Answers 4
- How do I pipe terminal standard output (stdout) to the clipboard?
- 5 Answers 5
- Copy current terminal prompt to clipboard
- Copy the contents of a file into the clipboard without displaying its contents
- 4 Answers 4
- X11
- GNU screen
- Apple OS/X
- Shells
- zsh CUTBUFFER/killring
What is the command line equivalent of copying a file to clipboard?
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.
This really doesn’t look like a duplicate to me. One answer is about general copy paste generally, and this one is about copy a file specific subject
How about a when a complete non-GUI Ubuntu is being used through ssh from macOS Terminal app or analogue?
4 Answers 4
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
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 $ -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:
How do I pipe terminal standard output (stdout) to the clipboard?
For example, Say I want to list the contents of a folder and directly paste them into a chat window for a friend to see. I realize I could do ls > filename.txt to create a file (filename.txt) with those contents; I’d then have to open or print the file and manually select and copy the text block (which can be annoying/tedious.) I clearly could also select and copy the output of ls directly from within the terminal window. It would be much faster/easier to simply pipe standard output to the clipboard. What terminal command allows me to do this?
5 Answers 5
This can be done with either xsel or xclip command line utilities. Since neither program comes with Ubuntu by default you’ll need to first install them via Ubuntu Software or the terminal. Here’s how in the terminal (but remember you only need one of these two.)
sudo apt install xsel sudo apt install xclip
Note: If you’re using Ubuntu in Windows Subsystem for Linux (WSL) see this other answer instead.
Now some examples. If you want to copy the output of ls to the clipboard here’s what you’d do:
This can of course be utilized for other terminal commands as well. Let’s say you want to paste your network info into a help forum.
sudo lshw -C network | xsel -ib
sudo lshw -C network | xclip -sel clip
Make this even easier with a new bash alias!
Edit your ~/.bash_aliases file (if it doesn’t exist yet create it first with touch ~/.bash_aliases )
Then add one (depending on which program you decided to go with) of the following:
Now (after restarting your terminal) you can send standard output to the clipboard just by piping it to ‘copy’ (or whatever you decide to name your new alias)
With newer Ubuntu versions, you should use apt . So sudo apt install xsel or sudo apt install xclip .
@jpaugh. Compare the Ubuntu package management documentation for 16.04 vs 14.04. In 16.04, it only mentions apt and not apt-get . You can still use apt-get , but they’re trying to encourage people to use apt .
If you also want to capture stderr (the errors that might occur with your command, besides stdout), you have to use e.g. ls nonexisting_file 2>&1 | xclip -sel clipboard , meaning you redirect stderr to stdout.
If you are attempting to copy to the clipboard using Ubuntu in Windows Subsystem for Linux (WSL) xsel or xclip will not work unless you are using X Windows as clipboard is only for graphical. However, to pipe terminal standard output to the clipboard in WSL Ubuntu you can use clip.exe . You can then paste into the WSL Ubuntu terminal with standard paste commands and the copied text will be available in Windows as well. For example,
will copy the current working directory to the (Windows) clipboard.
This search result appears at the top when looking for ways to copy/paste text in WSL so I think it is worthwhile to mention this so readers do not needlessly install xsel or xclip in Ubuntu and instead use clip.exe .
Copy current terminal prompt to clipboard
To cut, press ctrl + u . To paste, use ctrl + y . This copies whole line to bash clipboard. If you’re using X and default Ubuntu terminal, you can use your mouse to mark contents and press ctrl + shift + c to copy, and ctrl + shift + v to paste.
I am using Xubuntu and while ctrl+u and ctrl+y works in the terminal, I need to be able to cut/copy from terminal and paste in a different app, which doesn’t work. Possibly, ctrl+u cuts the text into terminal’s own clipboard, not the global one?
You have to use X’s clipboard. Try ctrl+shift+c and ctrl+shift+v. If you’re using terminal (without graphical UI), you’re out of luck. If you want to append commands to script file, you can use echo ping www.google.com >> filename.txt to work around it.
And tip: you can prefix commands easily by pressing ctrl+u, writing echo and then hitting ctrl+y to paste original command back.
- Add a # to the front of the command (so it becomes a comment)
- Run it
- Grab it from the history and pipe it to a clipboard utillity like xclip: history | tail -n 1 | sed «s/[[:digit:]]* //» | sed «s/^#//» | xclip
An easier alternative is to add echo in front of the command and pipe to xclip. $ echo
If you don’t mind using the mouse, just triple click on the line you want to copy then press Ctrl + Shift + C . You can then paste it with Ctrl + V .
Vi mode solution
Keyboard-only solution. All characters are copied exactly «as-is». Out of the box — pure shell solution, no dependencies (except xsel obviously).
- Write anything you want to the command line without pressing .
- Press followed by , . Whole line should now disappear. To be able to type again, press .
- Type name of the alias ( c2c in my example) and press .
- Press ,
then , .
Voilà. Deleted text can be now pasted anywhere by pressing + (GUI), or ++ (reasonable CLI). If Your CLI isn’t reasonable, make it so.
fzf solution
Some likes like vi mode, others not really. But I think fuzzy finder is must have. It doesn’t allow to do exactly what you’ve asked, it can do something better! Instead of «hitting up several times» fzf can browse command history with order of magnitude better efficiency. Moreover, it can be used inside my c2c alias to copy to clipboard. Whatever is typed to the terminal at the moment cannot be nicely copied (without mouse), but if something is in history, it is a matter of seconds to get it.
Copy the contents of a file into the clipboard without displaying its contents
How to copy the contents of a file in UNIX without displaying the file contents. I don’t want to cat or vi to see the contents. I want to copy them to clipboard so that I can paste it back on my windows notepad. I can’t copy the file from that server to another due to access restrictions.
if your file is huge the clipboard will fail anyway. When the access restrictions say, that you cannot read the file, you are lost of course. If you cannot copy the file because you can’t write the file you must ask yourself, if there is another destination you can write to.
You could, perhaps, edit the file on the system itself. You then wouldn’t need to copy the contents anywhere.
I just asked a related question since I can’t get xclip working when logging into Ubuntu from Git Bash on Windows: stackoverflow.com/q/60117294/470749
4 Answers 4
X11
If using X11 (the most common GUI on traditional Unix or Linux based systems), to copy the content of a file to the X11 CLIPBOARD selection without displaying it, you can use the xclip or xsel utility.
to store the content of file as the CLIPBOARD X11 selection.
To store the output of a command:
mycommand | xclip -sel c mycommand | xsel -b
Note that it should be stored using an UTF-8 encoding or otherwise pasting won’t work properly. If the file is encoded using an another character set, you should convert to UTF-8 first, like:
for a file encoded in latin1/iso8859-1.
xsel doesn’t work with binary data (it doesn’t accept null bytes), but xclip does.
To store it as a CUT_BUFFER (those are still queried by some applications like xterm when nothing claims the CLIPBOARD or PRIMARY X selections and don’t need to have a process running to serve it like for selections), though you probably won’t want or need to use that nowadays:
xprop -root -format CUT_BUFFER0 8s -set CUT_BUFFER0 "$(cat file)"
(removes the trailing newline characters from file ).
GNU screen
GNU screen has the readbuf command to slurp the content of a file into its own copy-paste buffer (which you paste with ^A] ). So:
Apple OS/X
Though Apple OS/X can use X11. It doesn’t by default unless you run a X11 application. You would be able to use xclip or xsel there as OS/X should synchronise the X11 CLIPBOARD selection with OS/X pasteboard buffers, but that would be a bit of a waste to start the X11 server just for that.
On OS/X, you can use the pbcopy command to store arbitrary content into pasteboard buffers:
(the file’s character encoding is expected to be the locale’s one). To store the output of a command:
Shells
Most shells have their own copy-paste buffers. In emacs mode, cut and copy operations store the copied/cut text onto a stack which you yank/paste with Ctrl-Y , and cycle through with Alt+Y
zsh CUTBUFFER/killring
In zsh , the stack is stored in the $killring array and the top of the stack in the $CUTBUFFER variable though those variables are only available from Zsh Line Editor (zle) widgets and a few specialised widgets are the prefered way to manipulate those.
Because those are only available via the ZLE, doing it with commands is a bit convoluted:
zmodload zsh/mapfile zle-line-init() < if [ -n "$FILE_TO_COPY" ]; then zle copy-region-as-kill $mapfile[$FILE_TO_COPY] unset FILE_TO_COPY fi >zle -N zle-line-init file-copy() FILE_TO_COPY=$1:A
The zle-line-init special widget is executed once at the start of each new command prompt. What that means is that the file will only be copied at the next prompt. For instance, if you do:
The file will only be copied after those 2 seconds.