命令行下载终极指南:15个curl与wget高级技巧让下载效率翻倍
【免费下载链接】Bash-OnelinerA collection of handy Bash One-Liners and terminal tricks for data processing and Linux system maintenance.项目地址: https://gitcode.com/GitHub_Trending/ba/Bash-Oneliner
在Linux系统管理和数据处理中,curl和wget是两款不可或缺的命令行下载工具。无论你是需要处理大批量文件下载、实现断点续传,还是进行API调试和网络请求分析,掌握这两款工具的高级用法都能让你的工作效率大幅提升。本文将深入解析15个实用场景,帮助你从基础用户进阶为下载专家。
基础功能对比:选择合适的工具
| 功能特性 | curl | wget |
|---|---|---|
| 协议支持 | HTTP/HTTPS/FTP/SCP/IMAP等 | HTTP/HTTPS/FTP |
| 文件输出 | 需要显式指定-o参数 | 自动使用URL文件名 |
| 递归下载 | 不支持原生递归 | 支持强大的递归下载 |
| 后台运行 | 需要配合nohup | 内置-b参数 |
| 断点续传 | 支持(-C -) | 支持(-c) |
| 脚本集成 | 更适合API调用 | 更适合批量下载 |
场景一:三步实现完美断点续传
问题场景:下载大文件时网络中断,重新下载浪费时间和带宽。
解决方案:
# curl断点续传:从上次中断处继续下载 curl -C - -O http://example.com/large_file.zip # wget断点续传:继续未完成的下载 wget -c http://example.com/large_file.zip参数解析:
-C -:curl自动检测断点位置-c:wget继续下载部分文件
场景二:快速配置限速下载
问题场景:下载文件占用全部带宽,影响其他网络应用。
解决方案:
# curl限速下载:限制为100KB/s curl --limit-rate 100K -O http://example.com/large_file.zip # wget限速下载:限制为200KB/s wget --limit-rate=200k http://example.com/large_file.zip场景三:批量下载自动化处理
问题场景:需要下载多个文件,手动操作效率低下。
解决方案:
# 方法1:将URL列表保存到文件,每行一个URL cat > urls.txt << EOF http://example.com/file1.zip http://example.com/file2.zip http://example.com/file3.zip EOF # curl批量下载(配合xargs) xargs -n 1 curl -O < urls.txt # wget批量下载(直接读取文件) wget -i urls.txt场景四:递归下载整站资源
问题场景:需要下载网站特定类型的所有文件。
解决方案:
# wget递归下载所有MP3文件 wget -r -l1 -H -t1 -nd -N -np -A mp3 -e robots=off http://example.com参数详解:
-r:启用递归下载-l1:递归深度为1级-A mp3:只下载MP3格式文件-e robots=off:忽略robots.txt限制
场景五:文件上传与共享
问题场景:需要通过命令行快速上传文件并生成分享链接。
解决方案:
# 使用transfer.sh服务上传文件 curl --upload-file ./document.pdf https://transfer.sh/document.pdf # 上传后会返回下载链接,如: # https://transfer.sh/abc123/document.pdf场景六:HTTP请求调试与分析
问题场景:需要检查API响应、分析重定向链或测量请求时间。
解决方案:
# 获取HTTP状态码 curl -s -o /dev/null -w "%{http_code}" https://api.example.com # 测量请求总时间 curl -v -o /dev/null -s -w 'Total: %{time_total}s\n' google.com # 获取重定向后的最终URL curl -s -o /dev/null -w "%{redirect_url}" https://short.url/example场景七:自定义请求头处理
问题场景:需要模拟特定浏览器或设置API认证头信息。
解决方案:
# 设置User-Agent模拟Chrome浏览器 curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/58.0.3029.110" http://example.com # 设置Referer来源 curl -e "http://referer.example.com" http://api.example.com/data场景八:代理服务器配置
问题场景:在公司网络或特定环境下需要通过代理服务器访问外部资源。
解决方案:
# curl通过代理下载 curl -x http://proxy-server:8080 -O http://example.com/file.zip # wget通过代理下载 wget -e use_proxy=yes -e http_proxy=http://proxy-server:8080 http://example.com/file.zip场景九:认证与登录处理
问题场景:需要访问需要用户名密码认证的资源。
解决方案:
# curl基础认证 curl -u username:password http://example.com/protected # wget基础认证 wget --user=username --password=password http://example.com/protected场景十:后台下载管理
问题场景:长时间下载任务需要放到后台执行,同时能够监控下载进度。
解决方案:
# wget后台下载 wget -b http://example.com/large_file.zip # 查看后台下载日志 tail -f wget-log疑难解答:常见问题快速解决
问题1:SSL证书验证失败
# 临时绕过SSL验证(仅测试环境使用) curl -k https://example.com wget --no-check-certificate https://example.com问题2:下载速度过慢
# 使用多连接加速下载 curl -Z http://example.com/large_file.zip # wget优化参数组合 wget -c -t 0 -w 2 --limit-rate=200k http://example.com/large_file.zip问题3:中文文件名乱码
# 设置字符编码 curl -O --remote-name-all http://example.com/中文文件.zip性能优化与最佳实践
连接复用优化
# curl启用连接复用 curl -Z --parallel-max 5 -O http://example.com/file1.zip -O http://example.com/file2.zip超时设置保护
# 设置连接超时和传输超时 curl --connect-timeout 10 --max-time 30 http://example.com/file.zip脚本集成实战案例
自动化下载脚本示例
#!/bin/bash # 下载函数 download_file() { local url=$1 local output=$2 if [[ $url == *"large"* ]]; then echo "下载大文件,启用断点续传..." wget -c -O "$output" "$url" else echo "下载普通文件..." curl -o "$output" "$url" fi } # 主程序 main() { # 读取URL列表 while IFS= read -r url; do filename=$(basename "$url") download_file "$url" "$filename" done < urls.txt } main "$@"总结与进阶建议
通过本文的15个实战场景,你已经掌握了curl和wget的高级应用技巧。在实际工作中,建议:
- 根据场景选择工具:API调试用curl,批量下载用wget
- 参数组合优化:合理搭配参数实现最佳效果
- 错误处理机制:为脚本添加适当的错误检查和重试逻辑
- 性能监控:定期检查下载速度和资源使用情况
记住,命令行工具的魅力在于灵活组合。将curl和wget与其他工具如grep、awk、xargs等结合使用,能够解决更加复杂的下载需求。
【免费下载链接】Bash-OnelinerA collection of handy Bash One-Liners and terminal tricks for data processing and Linux system maintenance.项目地址: https://gitcode.com/GitHub_Trending/ba/Bash-Oneliner
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考