Using wget to Download Files from an FTP Server
The wget command is a powerful, non-interactive network downloader available on Linux and other Unix-like systems. It supports HTTP, HTTPS, and FTP protocols, making it an excellent tool for retrieving files from FTP servers directly from the command line.
Basic Command Syntax
The fundamental syntax for downloading a file via FTP with wget is:
wget ftp://server_address/file_path --ftp-user=username --ftp-password=password
Parameter Explanation and Examples
Here is a breakdown of the command components:
- ftp://server_address/file_path: Specifies the FTP server's address and the full path to the file or directory you want to download. Examples:
ftp://192.168.1.100/public/report.pdforftp://example.com/data/archive.tar.gz. - --ftp-user=username: Specifies the username for authenticating with the FTP server.
- --ftp-password=password: Specifies the password for the given username.
Complete Example: To download the file /backups/database.sql from an FTP server at IP 10.0.0.5 using the username alice and password secret123, run:
wget ftp://10.0.0.5/backups/database.sql --ftp-user=alice --ftp-password=secret123
Advanced Usage and Important Notes
1. Recursive Directory Download: To download an entire directory and its contents, use the -r (recursive) flag.
wget -r ftp://10.0.0.5/public/ --ftp-user=alice --ftp-password=secret123
2. Security Warning: Including passwords directly on the command line is insecure, as they may be visible in process lists or shell history. Safer alternatives include:
- Reading the password from a file:
--ftp-password=`cat password_file`. - Using a
.netrcfile for authentication (recommended).
3. Using a .netrc File (Recommended): Create a file named ~/.netrc in your home directory with the following format to store credentials securely:
machine 10.0.0.5
login alice
password secret123
Set strict permissions: chmod 600 ~/.netrc. You can then download without exposing credentials in the command:
wget ftp://10.0.0.5/backups/database.sql
4. Passive Mode: If you encounter connection issues (often due to firewalls), try enabling passive FTP mode with the --passive-ftp option.
wget --passive-ftp ftp://10.0.0.5/file.zip --ftp-user=alice --ftp-password=secret123
5. Rate Limiting and Retries: Control bandwidth usage with --limit-rate and set retry attempts for network errors with -t.
wget -t 5 --limit-rate=200k ftp://10.0.0.5/largefile.iso --ftp-user=alice --ftp-password=secret123
Summary
wget is a reliable tool for downloading files from FTP servers in Linux. Use the basic syntax for simple tasks. For automation or improved security, integrate it with a .netrc file. Understanding options like recursive download, passive mode, and rate limiting allows you to handle diverse network environments and requirements effectively.