File Sharing From Linux Command Line
In this article
Yes, you can share files directly from the Linux command line using tools like scp, rsync, curl, and nc, or by running a simple HTTP server with python3 -m http.server. This article explains the most common methods, when to use each, and how to avoid the pitfalls that trip up new users.
Why Use the Command Line for File Sharing
Graphical file-sharing tools (like Dropbox or a USB drive) work fine, but the command line offers speed, scriptability, and control. You can copy files between servers, automate backups, or transfer data over an SSH connection without leaving the terminal. For administrators and developers, command-line tools are often the only option when working on headless machines or remote servers.
Core Tools and Their Use Cases
Different tools serve different purposes. Here is a quick breakdown:
- scp – Secure copy over SSH. Best for one-off file transfers between two machines.
- rsync – Synchronizes files and directories, transferring only the differences. Ideal for backups and large datasets.
- curl / wget – Download or upload files from/to web servers. Useful for fetching remote files or pushing to a web service.
- nc (netcat) – Raw TCP/UDP data transfer. Good for quick, unencrypted transfers on a trusted network.
- python3 -m http.server – Serves the current directory over HTTP. Perfect for sharing files with anyone on your LAN.
How to Use scp and rsync
scp is straightforward. To copy a file to a remote server:
scp /path/to/local/file user@remote:/path/to/destination
To copy from the remote to your local machine, reverse the order:
scp user@remote:/path/to/file /local/destination
rsync is more powerful. A typical backup command might look like:
rsync -avz /source/dir/ user@remote:/backup/dir/
The -a flag preserves permissions and timestamps, -v shows progress, and -z compresses data during transfer. The trailing slash on the source directory matters: it copies the contents, not the directory itself.
Serving Files Over HTTP
If you need to share files with someone who doesn't have SSH access, a quick HTTP server is handy. Navigate to the directory you want to share and run:
python3 -m http.server 8000
This serves the directory on port 8000. The other person can open http://your-ip:8000 in a browser and download files. To stop the server, press Ctrl+C. Note that this is not encrypted, so only use it on trusted networks.
Transferring Files with netcat
Netcat is a low-level tool that can send raw data. On the receiving machine, start a listener:
nc -l -p 1234 > received_file
On the sending machine, push the file:
nc -w 3 192.168.1.10 1234 < file_to_send
The -w 3 tells netcat to close the connection after three seconds of inactivity. This method is fast but unencrypted, so use it only on a private, trusted network.
Common Problems and How to Avoid Them
- Permission denied – Ensure the remote user has write permission to the destination directory. Check with
ls -ld /path. - Firewall blocking ports – For HTTP or netcat, open the port in your firewall (e.g.,
sudo ufw allow 8000on Ubuntu). - rsync and trailing slashes – A trailing slash on the source copies the contents; without it, the directory itself is copied. This is a frequent source of confusion.
- Large files timing out – Use
rsync --partialto resume interrupted transfers, or increase SSH timeout settings. - Wrong IP address – Check the target machine's IP with
ip addrorhostname -I.
Security Considerations
Always prefer encrypted methods like scp or rsync over SSH. Plain HTTP and netcat expose your data to anyone on the network. If you must use HTTP, consider adding a password via --auth (if supported) or use a tool like caddy for automatic HTTPS. Also, be careful with rsync to untrusted destinations – always double-check the destination path to avoid overwriting files.
Alternatives Worth Knowing
- sftp – An interactive file transfer protocol over SSH. Good for browsing remote directories.
- git – For sharing code or text files, a git repository can be a practical way to distribute and track changes.
- Magic Wormhole – A modern tool that uses a shared code to transfer files securely. Install with
pip install magic-wormhole. - Cloud CLI tools – Services like AWS S3 or Google Drive have command-line clients, but they require account setup.
Frequently Asked Questions
How do I share a file with someone on the same Wi-Fi?
Use python3 -m http.server in the directory containing the file, then give the other person your IP address and port. They can download it in a browser. Remember that this is unencrypted, so avoid sensitive data.
What is the fastest way to transfer a large directory?
rsync is usually the fastest because it only transfers changed parts. For a one-time transfer of a huge directory, tar piped over SSH can be efficient: tar czf - /path | ssh user@remote 'tar xzf - -C /destination'.
Can I resume an interrupted transfer?
Yes, with rsync --partial or --append. For scp, there is no built-in resume; you'd have to restart the transfer. For HTTP downloads, use curl -C - to resume.
How do I send a file to someone without an SSH account?
Set up a temporary HTTP server or use a tool like Magic Wormhole. Both allow one-way sharing without requiring a login on the receiving end.
Is it safe to use netcat over the internet?
No. Netcat sends data in plaintext and has no authentication. Anyone can intercept or inject data. Use it only on trusted local networks, and prefer SSH-based tools for anything sensitive.