下载文件wget

wget 命令完整指南

1. 基本命令格式

复制代码
wget [选项] [URL]

📋 常用选项详解

基础下载选项

选项 说明
-O <文件名> 指定下载文件的保存名称
-P <目录> 指定下载文件的保存目录
-c 断点续传(继续未完成的下载)
-b 后台下载
-q 安静模式(不显示输出)
-v 详细模式(显示更多信息)

连接和重试选项

选项 说明
--tries=<次数> 设置重试次数
--timeout=<秒> 设置超时时间
--wait=<秒> 设置下载间隔时间
--limit-rate=<速度> 限制下载速度

高级功能选项

选项 说明
-r 递归下载(整个网站)
-np 不追溯至父目录
-A <模式> 接受的文件模式(通配符)
-R <模式> 拒绝的文件模式
--user=<用户名> FTP/HTTP 用户名
--password=<密码> FTP/HTTP 密码

📁 实际使用场景示例

场景1:基本文件下载

复制代码
# 下载单个文件到当前目录
wget https://example.com/file.zip

# 下载并指定保存文件名
wget -O myfile.zip https://example.com/file.zip

# 下载到指定目录
wget -P /home/user/downloads/ https://example.com/file.zip

# 下载到指定目录并重命名
wget -O /home/user/downloads/myfile.zip https://example.com/file.zip

场景2:断点续传和后台下载

复制代码
# 断点续传(如果下载中断)
wget -c https://example.com/largefile.iso

# 后台下载(适合大文件)
wget -b https://example.com/largefile.iso
# 查看后台下载进度
tail -f wget-log

# 限制下载速度(单位:k/m 表示 KB/s 或 MB/s)
wget --limit-rate=500k https://example.com/largefile.iso

场景3:递归下载(整站镜像)

复制代码
# 递归下载整个网站
wget -r https://example.com/

# 递归下载,但不追溯至父目录
wget -r -np https://example.com/path/

# 只下载特定类型的文件
wget -r -A "*.pdf,*.doc" https://example.com/documents/

# 排除特定类型的文件
wget -r -R "*.jpg,*.png" https://example.com/

场景4:认证下载

复制代码
# HTTP 基础认证
wget --user=username --password=password https://example.com/protected/file.zip

# FTP 下载
wget --ftp-user=username --ftp-password=password ftp://example.com/file.zip

# 从输入读取密码(更安全)
wget --user=username --ask-password https://example.com/protected/file.zip

场景5:批量下载

复制代码
# 从文件读取 URL 列表进行批量下载
wget -i url_list.txt

# URL 列表文件内容示例:
# https://example.com/file1.zip
# https://example.com/file2.zip
# https://example.com/file3.zip

# 使用通配符批量下载
wget https://example.com/files/file{1..10}.zip

🔧 实用技巧和高级用法

1. 下载重定向文件

复制代码
# 有些链接需要跟随重定向
wget --trust-server-names https://example.com/download?file=123

# 或者手动指定重定向后的文件名
wget -O myfile.zip https://example.com/download?file=123

2. 处理 SSL/TLS 证书问题

复制代码
# 忽略 SSL 证书验证(不安全,仅测试用)
wget --no-check-certificate https://example.com/file.zip

# 指定自定义 CA 证书包
wget --ca-certificate=/path/to/cacert.pem https://example.com/file.zip

3. 伪装浏览器 User-Agent

复制代码
# 有些网站会检查 User-Agent
wget --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" https://example.com/file.zip

4. 限制递归深度

复制代码
# 只递归 2 层深度
wget -r -l 2 https://example.com/

# 镜像整个站点(保持目录结构)
wget -mk -w 10 https://example.com/

5. 定时下载

复制代码
# 等待 10 秒后开始下载
wget -w 10 https://example.com/file.zip

# 在特定时间下载(结合 cron 使用)
# 在 crontab 中设置:0 2 * * * wget https://example.com/backup.zip

wget 替代方案

1. curl(功能更丰富的传输工具)

复制代码
# 下载文件
curl -O https://example.com/file.zip

# 下载并重命名
curl -o myfile.zip https://example.com/file.zip

# 支持更多协议,更好的错误处理

2. aria2(多连接下载,速度更快)

复制代码
# 多连接下载
aria2c -x 16 https://example.com/largefile.iso

# 分段下载,加速大文件下载
aria2c -s 10 https://example.com/largefile.iso

🛠️ 故障排除和常见问题

问题1:证书验证失败

复制代码
# 解决方案1:更新 CA 证书
sudo apt update && sudo apt install ca-certificates  # Debian/Ubuntu

# 解决方案2:临时忽略(仅测试环境)
wget --no-check-certificate https://example.com/file.zip

问题2:权限被拒绝

复制代码
# 检查目标目录权限
ls -ld /target/directory

# 使用 sudo 或选择用户有权限的目录
wget -P ~/downloads/ https://example.com/file.zip

问题3:连接超时

复制代码
# 增加超时时间和重试次数
wget --timeout=60 --tries=10 https://example.com/file.zip

# 检查网络连接
ping example.com

问题4:403 Forbidden 错误

复制代码
# 尝试伪装 User-Agent
wget --user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36" https://example.com/file.zip

📋 实用脚本示例

批量下载脚本

复制代码
#!/bin/bash
# batch_download.sh

BASE_URL="https://example.com/files/"
FILE_LIST=("file1.zip" "file2.zip" "file3.zip" "file4.zip")

DOWNLOAD_DIR="./downloads"
mkdir -p "$DOWNLOAD_DIR"

for file in "${FILE_LIST[@]}"; do
    echo "正在下载: $file"
    wget -P "$DOWNLOAD_DIR" "$BASE_URL$file"
    
    # 添加延迟,避免对服务器造成压力
    sleep 2
done

echo "所有文件下载完成!"

网站镜像脚本

复制代码
#!/bin/bash
# mirror_site.sh

SITE_URL="$1"
MIRROR_DIR="./mirror_$(date +%Y%m%d)"

if [ -z "$SITE_URL" ]; then
    echo "用法: $0 <网站URL>"
    exit 1
fi

echo "开始镜像网站: $SITE_URL"
wget --mirror \
     --page-requisites \
     --html-extension \
     --convert-links \
     --adjust-extension \
     --restrict-file-names=windows \
     --no-parent \
     --directory-prefix="$MIRROR_DIR" \
     "$SITE_URL"

echo "网站镜像完成: $MIRROR_DIR"

使用示例:

复制代码
chmod +x mirror_site.sh
./mirror_site.sh https://example.com

💡 最佳实践建议

  1. 大文件下载 :使用 -c 参数支持断点续传
  2. 批量下载 :使用 -i 参数从文件读取 URL 列表
  3. 网站镜像 :使用 -mk--mirror 参数
  4. 生产环境 :避免使用 --no-check-certificate
  5. 礼貌下载 :使用 --wait--limit-rate 避免对服务器造成压力

记住:wget 非常适合自动化脚本和服务器环境下的文件下载任务!

相关推荐
科技小花34 分钟前
数据治理平台架构演进观察:AI原生设计如何重构企业数据管理范式
数据库·重构·架构·数据治理·ai-native·ai原生
一江寒逸36 分钟前
零基础从入门到精通MySQL(中篇):进阶篇——吃透多表查询、事务核心与高级特性,搞定复杂业务SQL
数据库·sql·mysql
D4c-lovetrain37 分钟前
linux个人心得22 (mysql)
数据库·mysql
阿里小阿希1 小时前
CentOS7 PostgreSQL 9.2 升级到 15 完整教程
数据库·postgresql
荒川之神1 小时前
Oracle 数据仓库雪花模型设计(完整实战方案)
数据库·数据仓库·oracle
做个文艺程序员1 小时前
MySQL安全加固十大硬核操作
数据库·mysql·安全
不吃香菜学java2 小时前
Redis简单应用
数据库·spring boot·tomcat·maven
一个天蝎座 白勺 程序猿2 小时前
Apache IoTDB(15):IoTDB查询写回(INTO子句)深度解析——从语法到实战的ETL全链路指南
数据库·apache·etl·iotdb
不知名的老吴2 小时前
Redis的延迟瓶颈:TCP栈开销无法避免
数据库·redis·缓存
YOU OU2 小时前
三大范式和E-R图
数据库