Chapter 21: File Transfers

In this chapter, we’ll explore file transfer tools that allow you to move files between systems efficiently and securely. You’ll learn how to use tools like scp, rsync, lftp, and ncftp to copy files, synchronize directories, and manage remote file transfers. By the end of this chapter, you’ll be able to transfer files like a pro.


1. Why Learn File Transfer Tools?

File transfer is a common task in system administration, development, and data management. Whether you’re backing up data, deploying software, or sharing files, these tools make the process fast, reliable, and secure.


2. Secure Copy with scp

scp (Secure Copy) is a command-line tool for securely copying files between local and remote systems using SSH.

Basic Usage

  • Copy a file from local to remote:
    $ scp file.txt user@remote_host:/path/to/destination/
  • Copy a file from remote to local:
    $ scp user@remote_host:/path/to/file.txt /local/destination/
  • Copy a directory recursively:
    $ scp -r directory/ user@remote_host:/path/to/destination/

Example

Copy a file to a remote server:

$ scp report.pdf amar@192.168.1.100:/home/amar/documents/

3. Synchronizing Files with rsync

rsync is a powerful tool for synchronizing files and directories between systems. It’s efficient because it only transfers changes.

Basic Usage

  • Sync a directory to a remote host:
    $ rsync -avz /local/directory/ user@remote_host:/remote/directory/
  • -a: Archive mode (preserves permissions, timestamps, etc.).
  • -v: Verbose output.

-z: Compress data during transfer.

Sync from a remote host to local:
$ rsync -avz user@remote_host:/remote/directory/ /local/directory/

Dry run (simulate the sync):
$ rsync -avz --dry-run /local/directory/ user@remote_host:/remote/directory/

Example

Sync a local directory to a remote server:

$ rsync -avz ~/projects/ amar@192.168.1.100:/backup/projects/

4. Using lftp for Advanced File Transfers

lftp is a sophisticated FTP client that supports multiple protocols (FTP, HTTP, SFTP, etc.) and advanced features like parallel transfers and scripting.

Basic Usage

  • Connect to an FTP server:
    $ lftp ftp://user:password@ftp.example.com
  • Download a file:
    $ lftp -e "get file.txt; quit" ftp://user:password@ftp.example.com
  • Mirror a directory:
    $ lftp -e "mirror /remote/directory/ /local/directory/; quit" ftp://user:password@ftp.example.com

Example

Download a file from an FTP server:

$ lftp -e "get report.pdf; quit" ftp://user:password@ftp.example.com

5. Using ncftp for User-Friendly FTP Transfers

ncftp is a user-friendly FTP client with features like bookmarking and recursive directory downloads.

Basic Usage

  • Connect to an FTP server:bash ncftp ftp.example.com
  • Download a file: bash get file.txt
  • Download a directory recursively:bash get -R directory/

Example

Download a file from an FTP server:

$ ncftp ftp.example.com
$ get report.pdf
$ quit

6. Practical Examples

Backup a Directory to a Remote Server

Use rsync to back up a local directory to a remote server:

$ rsync -avz ~/documents/ amar@192.168.1.100:/backup/documents/

Download a File from an FTP Server

Use lftp to download a file:

$ lftp -e "get file.txt; quit" ftp://$ user:password@ftp.example.com

Sync a Remote Directory to Local

Use rsync to sync a remote directory to your local machine:

$ rsync -avz amar@192.168.1.100:/remote/projects/ ~/local/projects/

More options for file transfers

For efficient file transfers across multiple devices and cloud services, rclone supports various cloud storage systems, allowing you to sync or copy files as if dealing with a local filesystem. 

Syncthing enables secure, peer-to-peer file synchronization between personal machines. For large file transfers over unreliable connections, rsync and wget’s –continue option ensure resumable transfers, minimizing wasted bandwidth and time. These modern approaches are essential for managing files across different systems and cloud providers.

$ rclone copy /local/path remote:path
$ rsync --partial /source/ /destination/

curl

  • Purpose: Transfer data from or to a server. It supports various protocols like HTTP, HTTPS, FTP, and more.
  • Examples:
  • Fetch a Webpage: $ curl https://example.com
  • Download a File: $ curl -O https://example.com/file.zip
  • Send Data via POST Request
    $ curl -d "param1=value1&param2=value2" -X POST https://example.com
  • Follow Redirects: $ curl -L https://example.com

wget

  • Purpose: Download files from the web. It’s particularly useful for downloading large files or entire websites.
  • Examples:
  • Download a File: $ wget https://example.com/file.zip
  • Download an Entire Website: $ wget --mirror https://example.com
  • Resume a Partial Download: $ wget -c https://example.com/file.zip

 


Extra: Network Tools (Web Browsing and Email)

In this chapter, we’ll explore command-line tools for web browsing, email, and network diagnostics. These tools allow you to interact with the web, manage emails, and troubleshoot network issues directly from the terminal. By the end of this chapter, you’ll be able to efficiently perform network-related tasks using powerful command-line utilities.


1. Introduction to Network Tools

The command line is not just for system administration—it’s also a powerful platform for interacting with networks. Whether you’re downloading files, sending emails, or diagnosing network issues, these tools will help you get the job done quickly and efficiently.


2. Web Browsing Tools

lynx

  • Purpose: A text-based web browser that allows you to browse the web directly from the terminal.
  • Examples:
  • Browse a Website: $ lynx https://example.com
  • Search for Text on a Page:
    • Press / and type your search term.
  • Navigate Links:
    • Use the arrow keys to move between links and press Enter to follow them.

3. Email Tools

mutt

  • Purpose: A text-based email client that supports sending, receiving, and managing emails.
  • Installation: bash $ sudo apt install mutt # Debian/Ubuntu $ sudo dnf install mutt # Red Hat/Fedora
  • Examples:
  • Open mutt: bash $ mutt
  • Send an Email:
    • Press m to compose a new email.
    • Fill in the recipient, subject, and body.
    • Press y to send.
  • Read Emails:
    • Use the arrow keys to navigate emails and press Enter to read.

mail

  • Purpose: A simple command-line tool for sending and receiving emails.
  • Installation: bash $ sudo apt install mailutils # Debian/Ubuntu $ sudo dnf install mailx # Red Hat/Fedora
  • Examples:
  • Send an Email: bash $ echo "Hello" | mail -s "Subject" user@example.com
  • Read Emails: bash $ mail
  • Delete an Email:
    • While in the mail interface, type d followed by the email number.

 


By mastering these network tools, you can: - Browse the web and download files using curl, wget, and lynx. - Send and receive emails using mutt and mail. - Diagnose network issues using ping, traceroute, and netstat. - Securely connect to remote servers using ssh.


Practice Time!

Let’s put your new skills to the test: 
1. Use scp to copy a file to a remote server. 
2. Use rsync to synchronize a local directory with a remote backup. 
3. Use lftp to download a file from an FTP server. 
4. Use ncftp to explore an FTP server and download a directory. 
5. Fetch a Webpage: - Use curl to fetch the content of a webpage. 
6. Download a File: - Use wget to download a file from the web. 
7. Send an Email: - Use the mail command to send an email. 
 


That’s it for Chapter 21! You’ve now learned how to use file transfer tools like scp, rsync, lftp, and ncftpto move files between systems efficiently and securely. In the next chapter, we’ll dive into Linux security basics—how to secure your system using SSH, firewalls, and encryption. Until then, practice transferring files to become more comfortable with these tools.


Prev: Chapter 20 | Next: Chapter 22