Netcat Section 3 of 14

File Transfer

Netcat can transfer files between systems by treating the network connection as a simple data pipeline. One system listens while the other connects and sends data.

Basic File Transfer

To transfer a file, set up a listener on the receiving system, then connect from the sending system and redirect the file as input.

Receiver (Listening)

On the system that will receive the file:

nc -l 4444 > received_file.txt

Listens on port 4444 and saves all received data to received_file.txt

Sender (Connecting)

On the system that has the file to send:

nc <receiver_ip> 4444 < secret_document.pdf

Connects to the receiver and sends secret_document.pdf as input

Receiving Compressed Archives

You can combine Netcat with compression tools for efficient transfer of large files or directories.

Transfer a Directory

Using tar to package and compress a directory before transfer:

# On receiver: nc -l 4444 | tar -xz
# On sender: tar -cz /path/to/directory | nc <receiver_ip> 4444

Transfer with Progress Monitoring

Using pv (pipe viewer) to monitor transfer progress:

# On receiver:nc -l 4444 > backup.tar
# On sender: pv backup.tar | nc <receiver_ip> 4444

Bidirectional Communication

Netcat can also facilitate two-way communication for simple chat or command execution:

Simple Chat Setup

Two systems can chat by each listening on a port and connecting to the other:

# On both systems: nc -l 5555

Each system types messages that appear on the other's screen