128

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, 2015 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, 2015 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, 2015 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, 2020 at 16:19
  • Exactly same as stackoverflow.com/questions/5130968/…
    – limido
    Feb 16, 2021 at 9:05

4 Answers 4

139

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
}

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.

4
  • 5
    I'm facing this error: Error: Can't open display: (null)
    – Idemax
    Dec 12, 2018 at 18:48
  • Same problem here in fedora 7 Apr 4, 2019 at 16:48
  • 2
    @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. May 19, 2019 at 11:43
  • What if I'm in a docker container :( May 4, 2021 at 8:50
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, 2015 at 13:48
1

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

#! /bin/sh -
exec xclip -selection clipboard -i "$@"

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

1
  • noob question: And where do you put this script? (In which folder?)
    – Pranav
    Jul 21, 2021 at 6:41
1

Perhaps my solution is specific to my setup, but in any rate I'll put it here and see if its helpful for anyone else.

I have ssh access to the (Linux) server that has a file that I'd like to have in my local (OSx) computer's "clipboard" so that I can then paste it locally.

From the local machine I issue the following command:

$ ssh user@host 'cat /path/to/file/I/want/in/remote/system' | pbcopy 

Now the contents of the file are in my local buffer and I can paste into an application of my choice. Note that the contents of the file do not appear on the screen when I do this and the content is transmitted via ssh so should be as secure as copying the file via scp (in case there are concerns about privacy/security.)

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.