Transferring files with Netcat/nc

Transferring files with Netcat/nc

πŸ“… 2022-05-23 ✏️ 2026-07-11 ✍️ Andreas Wittmann πŸ‘οΈ ... nc netcat

Netcat (nc) is a lightweight tool for transferring data over TCP or UDP. It requires no daemon, no configuration, and is available on virtually every Linux system β€” useful for quick transfers on a local network where scp or rsync would be overkill.

Transferring a single file

Start the receiver first, then the sender.

Receiver:

nc -l -p 8888 > destination_file

Sender:

nc receiver-host 8888 < source_file

The receiver listens on TCP port 8888 and writes everything it receives into destination_file. The sender connects and pipes source_file into the connection. Once the transfer is complete, both sides exit.

The -w flag sets a timeout in seconds β€” useful to avoid the receiver hanging indefinitely if the sender disconnects unexpectedly:

nc -l -p 8888 -w 10 > destination_file

Transferring a directory

Combine netcat with tar to transfer entire directory trees in one step.

Receiver:

nc -l -p 8888 | tar xvf -

Sender:

tar cvf - ./mydir | nc receiver-host 8888

The sender packs the directory into a tar stream and pipes it directly into netcat. The receiver unpacks the stream on the fly β€” no intermediate archive file is created on either side.

With compression

Add gzip to reduce transfer time for compressible data:

Receiver:

nc -l -p 8888 | gunzip > destination_file

Sender:

gzip -c source_file | nc receiver-host 8888

For directories:

# Sender
tar cvf - ./mydir | gzip | nc receiver-host 8888

# Receiver
nc -l -p 8888 | tar xzvf -

With encryption

Netcat transmits data in plain text. For transfers over untrusted networks, pipe through openssl:

Receiver:

nc -l -p 8888 | openssl enc -d -aes-256-cbc -pbkdf2 > destination_file

Sender:

openssl enc -aes-256-cbc -pbkdf2 -in source_file | nc receiver-host 8888

Both sides will be prompted for the same passphrase. For unattended use, the passphrase can be passed via -pass pass:yourpassphrase, though this exposes it in the process list.

Notes