如何基于FTP服务器压缩包内的路径列表批量下载图片?
Great question! Batch downloading those images from FTP based on your TXT file is totally doable with a few simple scripting steps. Here are three reliable methods you can use, depending on your operating system and tools available:
Method 1: Standard FTP Client with Batch Script (Cross-Platform)
This uses the built-in FTP client that comes with most operating systems:
- First, extract the TXT file from your compressed package. Let’s assume the extracted file is named
image_paths.txt. - Generate a list of FTP
getcommands from the TXT file. Each line has the local filename first, then the remote path—we only need those two fields:- On Linux/macOS, run this in the terminal:
awk '{print "get " $2 " " $1}' image_paths.txt > ftp_commands.txt - On Windows (PowerShell):
Get-Content image_paths.txt | ForEach-Object { $parts = $_.Split(); "get $($parts[1]) $($parts[0])" } | Out-File -Encoding ASCII ftp_commands.txt
- On Linux/macOS, run this in the terminal:
- Edit the
ftp_commands.txtfile to add server connection details at the top, and a quit command at the end:open your-ftp-server.com your-ftp-username your-ftp-password # (all the generated get commands will be here) quit - Run the script:
- Linux/macOS:
ftp -n < ftp_commands.txt - Windows:
ftp -s:ftp_commands.txt
- Linux/macOS:
Note: Storing passwords in plain text is insecure. For safer access, use a .netrc file (Linux/macOS) or configure saved FTP credentials (Windows).
Method 2: Using lftp (Linux/macOS/WSL)
lftp is a powerful, script-friendly FTP client that handles batch operations smoothly. Install it first with sudo apt install lftp (Debian/Ubuntu) or brew install lftp (macOS):
- Extract your
image_paths.txtfile. - Run this command (replace placeholders with your FTP details):
lftp -u your-username,your-password your-ftp-server.com -e "$(awk '{print "get " $2 " " $1}' image_paths.txt); quit"
You can omit the password from the command to be prompted for it, or store credentials securely in ~/.lftprc.
Method 3: Using curl (Cross-Platform)
curl works with FTP and is widely available across systems:
- Extract
image_paths.txt. - Run this loop to download each file:
- Linux/macOS:
while read -r local_file remote_path hash; do curl ftp://your-username:your-password@your-ftp-server.com$remote_path -o "$local_file" done < image_paths.txt - Windows PowerShell:
$ftpPrefix = "ftp://your-username:your-password@your-ftp-server.com" Get-Content image_paths.txt | ForEach-Object { $parts = $_.Split() curl "$ftpPrefix$($parts[1])" -o $parts[0] }
- Linux/macOS:
For better security, use curl --netrc to pull credentials from a .netrc file instead of hardcoding them.
内容的提问来源于stack exchange,提问作者Xullien




