Copy file to buffer linux

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

Читайте также:  Прочитать linux раздел windows

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.

Источник

Linux — copy/cut file into clipboard

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.

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.

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

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:

Читайте также:  Hp laserjet 400 mfp m425dn driver linux

Источник

How can I load a file’s contents into the clipboard?

I have a files where I need to copy their contents to another file or into an application and rather than open it up, select all the text then copy and paste I’d like to know if I can effectively cat the file contents into the clipboard. Is this possible? A Windows program would be fine but something that works on Linux would be useful too. I don’t use a Mac but it might be useful to others. Bonus points if this can be done on the command line.

Aha, I knew it was asked before, for Windows: «How to pipe text from command line to the clipboard» at superuser.com/questions/97762/… Maybe leave this open for Linux then?

Suburb. Looks like I can use clip.exe for this. Would still like a non-command line version though. Maybe via Windows Explorer context menu?

7 Answers 7

Since you didn’t ask about Macs: cat file | pbcopy (and likewise pbpaste ) for those.

xclip (probably available in your Linux system’s repos) will work for any X11 system, including most Linux versions, or even X being run under windows or Mac OSX.

Example usage: xclip -sel clip < ~/.ssh/id_rsa.pub

In Linux, you can use xsel to mimic pbcopy and pbpaste :

alias pbcopy='xsel --clipboard --input' alias pbpaste='xsel --clipboard --output' 

On Linux and possible other systems which support xclip :

xclip -i -selection c file_to_copy_to_clipboard.txt 

I saw @JustinSmith also mentioned xclip but was missing an example, so had to look it up myself.

Another useful one: paste your clipboard to a file.

xclip -o -selection c > file_to_paste_to.txt 

use the command «type» as an equivalent to «cat» in windows to pipe the files content in text format into stdout (standard output) for that is the Terminal/prompt emulator you’re using (CMD in windows). So you can combine the commands into something like this :

(note this is the pipe operator, not file redirect) now the content of myFile.txt is transferred to the clipBoard buffer (I think it’s just a buffer since it’s not linux ). It’s also a global value , so one value is held at a time OS wide. So that’s for the «copy» feature , now for the «paste» :

  • Either you want to append to an existing file the values of the CLIP like usual stuff: type clip >> target.txt (or your target file — will add data without deleting the existing one inside that target file)
  • Or , you want to add/create a new file with the values of the CLIP like : type clip > target.txt (or your target file — will add data or OVERWRITE means deleting the existing one inside that target file)
Читайте также:  Linux cron as root

Use this program f2clip. Run it from the command line. It copies file contents into clipboard. I use it for copying text files into web browser for further processing. Download it from http://smrz.xf.cz/f2clip.exe or write your own from this source (it’s ugly):

 program f2clip; uses SysUtils, clipbrd; var i,r:integer; s:string; f:file; buf:array[0..1024*1024-1] of byte; data:string; d:pointer; begin try < TODO -oUser -cConsole Main : Insert code here >if (paramcount=0) then begin writeln('parameters: f2clip filename.txt'); end else begin write('parameter count: '); writeln(paramcount); for i:=1 to paramcount do begin s:=paramstr(i); writeln('file: ',s); assignfile(f,s); reset(f,1); BlockRead(f,buf,1024*1024,r); writeln('size: ',r); buf[r]:=0; d:=@(buf[0]); data:=PAnsiChar(d); Clipboard.AsText := data; close(f); end; end; except on E:Exception do Writeln(E.Classname, ': ', E.Message); end; end. 

Источник

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:

Источник

Оцените статью
Adblock
detector