Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using something like this to send file from one computer to another:

To serve file (on computer A):

cat | nc -l -p 1234

To receive file (on computer B):

netcat 1234 >

My question is... can I do the opposite? Let's say I have file on computer B and I want to send it to A but not the way I wrote above, but by making computer that's supposed to receive file (A) be 'listening' server and connect computer that's 'sending' file (B) to server and send the file? Is it possible? I think it might be but I'm not sure how to do this.

In case my above explanation is messed up: How do I send file TO 'server' instead of serving the file on server and then taking it FROM it (like I did above)?

share|improve this question
up vote 12 down vote accepted

On your server (A):

nc -l -p 1234 -q 1 > < /dev/null
On your "sender client" (B):
cat | netcat 1234

share|improve this answer
Yeah, I thought that I'll have to switch sides somehow but I wasn't sure how :). Thanks, it works! – Phil Jan 21 '10 at 4:23
That's a cute netcat trick. – DaveParillo Jan 21 '10 at 16:09
why the < /dev/null part? – törzsmókus Nov 27 '12 at 11:17
Because netcat both reads stdin and writes stdout simultaneously, sending anything read from stdin out to the network and printing anything received from the network on stdout. The </dev/null ensures that, when the received file has completed, netcat doesn't sit there indefinitely waiting for input from the console (which it shouldn't be reading anyway!) and also ensures that it doesn't gobble up any input that may be available from the terminal, such as you typing the next command you wanted to run. – martinwguy Jan 4 '13 at 7:18

As a note, if you want to also preserve file permissions, ownership and timestamps, we use tar with netcat to do transfers of directories and files.

On receiving system:

nc -l -p 12345 -q 1 | tar xz -C /path/to/root/of/tree

From sending system:

tar czf - ./directory_tree_to_xfer | nc <host name or IP address of receiving system> 12345 

Hope that helps.

share|improve this answer
Good addition to the answer, but please leave it as a comment and not as a separate answer. – agtoever Feb 27 '15 at 0:33
Couldn't add a comment above due to lack of Rep points on this sub-board, so I had to offer it as an answer, though apparently, you are allowed to comment on your own answers. – B.Kaatz Mar 12 '15 at 23:30

Start another instance of netcat on computer B. Just do what you did on computer A, but serve it from B. Give the new server a new port.

share|improve this answer
I don't want to do the same, I want to change commands to something else (i'm not sure if it's possible)... look at bold text in my question. What I mean is that I don't want to serve from B. I want A to be server listening for input... and then I want to send file contents from non-listening B to listening A. – Phil Jan 20 '10 at 5:51
That's not how netcat works. – DaveParillo Jan 20 '10 at 15:14
As you can see from accepted answer - yes, that's how netcat can work too. – Phil Jan 21 '10 at 4:24
You're right. I learn something new every day! – DaveParillo Jan 21 '10 at 16:10

Computer A: nc -l -p 1234 > filename.txt

Computer B: nc 1234 < filename.txt

Should work too ;)

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .