How to Use wget for Recursive FTP Directory Downloading: Complete Guide with Examples


2 views

When migrating web content between servers, many developers encounter the frustrating limitation of having only FTP access to the source server. Traditional methods like tar/zip archives might be unavailable due to permission restrictions, making recursive FTP downloads the only viable option.

The basic command you tried:

wget -r ftp://username:password@ip.of.old.host

fails because:

  • It defaults to the FTP root directory (often /home/admin)
  • Without path specification, it creates an artificial index.html
  • Missing critical FTP-specific parameters

Here's the proper syntax for recursive FTP downloads:

wget -r -nH --cut-dirs=1 --no-parent \
--user=username --password=password \
ftp://ip.of.old.host/var/www/html/

Key parameters explained:

  • -r: Recursive download
  • -nH: Disable host-prefixed directories
  • --cut-dirs=1: Skip the first directory level
  • --no-parent: Prevent ascending to parent directories

For large directory structures:

wget -m -nH --cut-dirs=2 --limit-rate=500k \
--user=ftpuser --password='complexPass123!' \
ftp://ftp.example.com/public_html/2023/

Mirroring with timestamp preservation:

wget -m -N --ftp-user=admin --ftp-password=secret \
ftp://192.168.1.100/var/www/
  • Connection timeouts: Add -T 60 --retry-connrefused
  • Passive mode problems: Use --no-passive-ftp or --passive-ftp
  • SSL/TLS FTP: Requires --ftp-ssl or --ftp-ssl-control

For more robust FTP operations:

lftp -u username,password ftp://server.com -e \ 
"mirror --parallel=5 --verbose /remote/path /local/path; quit"



When migrating web content between servers with only FTP access, developers often face difficulties with recursive downloads. The standard wget command behaves differently with FTP compared to HTTP, requiring specific parameters for proper directory traversal.

The basic recursive wget command:

wget -r ftp://username:password@host

often fails to download the actual directory structure, instead creating artificial index.html files. This occurs because:

  • FTP directory listings return different responses than web servers
  • wget's default FTP behavior prioritizes mirroring HTML content
  • The starting directory may not match your target path

For reliable recursive FTP downloads, use this enhanced command structure:

wget --ftp-user=USERNAME --ftp-password=PASSWORD \
     -r -nH --cut-dirs=N --no-parent \
     ftp://host.example.com/path/to/directory/

Key parameters explained:

-r          : recursive download
-nH         : disable host-prefixed directories
--cut-dirs=N: skip N leading directory components
--no-parent : prevent ascending to parent directories

Case 1: Downloading /var/www/html from server root

wget --ftp-user=admin --ftp-password=secret \
     -r -nH --cut-dirs=2 --no-parent \
     ftp://oldserver.example.com/var/www/html/

Case 2: Mirroring with original timestamps

wget --ftp-user=dev --ftp-password=access123 \
     -m -nH --cut-dirs=1 \
     ftp://ftp.example.com/public_html/

Resuming interrupted transfers:

wget --ftp-user=user --ftp-password=pass \
     -c -r -nH \
     ftp://backup.example.com/data/

Excluding specific file types:

wget --ftp-user=web --ftp-password=transfer \
     -r -nH --reject "*.tmp,*.bak" \
     ftp://legacy.example.com/site_backup/
  • Connection timeouts: Add --tries=10 --waitretry=60
  • SSL/TLS connections: Include --ftp-ssl or --ftp-ssl-control
  • Passive mode issues: Try --no-passive-ftp