114

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.

5
  • OK, if so is there an easy way to copy a very huge file. I got like 1000 lines. – Web Nash Jun 24 '15 at 12:41
  • 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. – ikrabbe Jun 24 '15 at 12:45
  • You could, perhaps, edit the file on the system itself. You then wouldn't need to copy the contents anywhere. – roaima Jun 24 '15 at 15:16
  • 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 – Ryan Feb 7 '20 at 16:19
  • Exactly same as stackoverflow.com/questions/5130968/… – limido Feb 16 at 9:05
125

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.

xclip -sel c < file

Or:

xsel -b < file

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:

<file iconv -f latin1 -t utf8 | xclip -sel c

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:

screen -X readbuf file

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:

pbcopy < file

(the file's character encoding is expected to be the locale's one). To store the output of a command:

mycommand | pbcopy

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
}
mycommand | pbcopy
mycommand | pbcopy
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:

file-copy file; sleep 2

The file will only be copied after those 2 seconds.

3
  • 4
    I'm facing this error: Error: Can't open display: (null) – Marcelo Filho Dec 12 '18 at 18:48
  • Same problem here in fedora 7 – Michael Pacheco Apr 4 '19 at 16:48
  • 1
    @MarceloFilho, xclip and xsel are X11 utilities, they manipulate the X11 selections, so can only be used in a X11 environment where $DISPLAY is set. – Stéphane Chazelas May 19 '19 at 11:43
3

Maybe you wanted to ask this. But any file you want to read must be opened by some system call. If you don't have access to a file, you can't read it. That's the idea of restricted access.

1
  • So whats your actual problem? If you can read but you can't copy, you might not be able to write. But when you want to copy you need to write the data somewhere. – ikrabbe Jun 24 '15 at 13:48
1

I installed xclip and created a script named "copy" that takes a filename as its argument.

#! /bin/bash
xclip -selection clipboard -i $@

Example usage: copy id_rsa.pub copies my public key to the clipboard.

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.