One-shot file transfer from the command line using netcat

Netcat is an extremely useful and versatile command line utility for computer networking. I’ve used it occasionally over the last few years for simple network operations like file transfer or getting two applications to talk to each other over the network.

Today, while I was trying to remember how to use it to transfer a file from a Linux computer to a Windows computer, I discovered the following gem of an example on Netcat’s Wikipedia page (accessed 22-10-2011). It sets up a “one-shot” transfer of a single file via Netcat on the source computer and a normal web browser on the destination computer.

{ echo -ne "HTTP/1.0 200 OK\r\n\r\n"; cat tea.png; } | nc -l 8080

I’ve modified the command provided on the Wikipedia page slightly so that it works on the Netcat version available by default on Ubuntu 11.04. In the above example, the file transferred is tea.png. The first part of the command, which is enclosed in curly braces prepends a HTTP header to the file contents and streams the whole lot into a pipe. The pipe directs the file (with HTTP header attached) into Netcat (the “nc” command), which listens for an incoming network connection on port 8080. Once a connection is initiated from the receiving computer, the file (with HTTP header attached) will be streamed over the network.

To receive the file on the destination computer, the IP address of the source computer is required. This can be obtained by typing ifconfig. My computer’s IP address (on the local network) is and the example command shown above specifies port number 8080.

To receive the file on the Windows computer, simply open a browser and enter the IP address and port number of the source computer into the address bar. As soon as you press return, the file will be downloaded. Here’s how it looked when I received my example file (the teapot image, tea.png) in Firefox.

Once the file has been downloaded once, the Netcat command will terminate and the file will no longer be available.

This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s