Using netcat and tar for network file transfer

Imagine you are on lan party or on the road and quickly want to transfer a file or directory to another computer. Both computer owners are just to lazy to setup something like ftp, smb, nfs. A very simple and even cross platform solution is using netcat and in case of a directory in combination with tar like the following steps. I will just show you how to use it without compression for a directory. Fell free to play around. You can test it locally of course.

1. The sender

The sender has to call netcat in server mode and pipe content into it. The next line tells tar to build a tarball and write it to standard output which is redirected via a pipe to netcat. Netcat is told to start in server mode (-l), listen on port 7878 (-p 7878) and shutdown itself after waiting 10 seconds after having seen an end of file in standard input (-q 10):

$ tar c directory | nc -q 10 -l -p 7878

2. The receiver

The receiver has to call netcat and tell him to connect to the remote machine and the correct port and redirects the standard output to a file. For convenience he also sets a timeout parameter (-w 10):

$ nc -w 10 remotehost 7878 > nameoftar.tar

That’s all. You just setup a very fast file transfer. For testing purposes use localhost. Please note that on the receiver side you are completely free to choose a file name for the .tar file. If you use something like gzip or bzip2 compression you should choose something like .tar.gz or .tar.bz2 of course.

Thanks to mnemonik pointing this out.

Feel free to add your file transfer quick hack here.