Linux 上 wget 用法详解
最近在网上查资料时,发现一个网站里有好多PDF文档,都想下载下来,手动一个一个点有点麻烦,于是就研究了如何用一条命令下载一个网站里所有PDF文件,这里记录下。
linux下使用wget命令可以完成此需求,递归下载一个网站下面,所有pdf文件命令如下:
wget -A pdf -c -np -L -r https://xxxxxxxxxx
简单解释下:
-A 后跟下载的文件类型或者格式,如pdf,txt等,多个样式用逗号分隔。
-c 断点续传,当网络环境不好,或者下载的文件比较多,或者下载的文件比较大时,最好加上这个。
-np 不向上递归到父目录
-L 只递归相对链接,不会到其它站点
-r 递归下载
wget是linux下很强大的一个工具,下面我们详细介绍下wget的使用。
目录
一、日志输出相关
1、日志信息输出到文件
2、不输出日志信息
3、不输出下载详情信息
4、输出debug日志信息
二、下载相关参数设置
1、后台下载
2、从文本文件读取url并下载
4、设置重试次数
4、下载时指定下载下来的文件在本地的文件名
5、下载时跳过已存在的文件
6、断点续传
7、文件有更新时下载
8、下载时显示服务器的回应包信息
9、设置超时时间
10、设置重试前等待时间
11、下载限速
三、设置下载目录
1、不根据url生成层级目录
2、下载文件到根据url路径强制生成的目录下
3、下载文件到指定目录下
四、http相关设置
1、设置http用户名密码
2、禁用服务器cache缓存功能
3、其它扩展功能
五、用户代理相关设置
六、https(tls/ssl)相关设置
七、ftp相关设置
八、递归下载参数
1、递归下载url里的所有链接
2、递归下载层级
3、转换相对路径(链接)文件
4、只下载/忽略下载指定类型文件
5、递归下载时不向上搜索到父目录
一、日志输出相关
1、日志信息输出到文件
-o, --output-file=FILE log messages to FILE.-a, --append-output=FILE append messages to FILE.
-o my_download.txt 是将日志输出到my_download.txt, 这里是小写字母‘-o’。
-a my_download.txt 是将日志追加到my_download.txt
2、不输出日志信息
-q, --quiet quiet (no output).
-q使wget下载处于静默模式。
3、不输出下载详情信息
-v, --verbose be verbose (this is the default).-nv, --no-verbose turn off verboseness, without being quiet.--report-speed=TYPE Output bandwidth as TYPE. TYPE can be bits.
wget默认是打印下载详细日志,可以使用wget -nv关闭该功能。
4、输出debug日志信息
wget -d会打印出wget的debug日志,输出的内容比较详细。
二、下载相关参数设置
1、后台下载
-b, --background go to background after startup.
比如下面我下载了个210MB的tar文件:
wget -b url之后,wget会至于后台执行,可以通过wget-log文件查看下载进度:
2、从文本文件读取url并下载
-i, --input-file=FILE download URLs found in local or external FILE.
预先将要下载的文件链接写入文本文件中,每个链接一行,然后使用wget -i download.link来批量下载文件:
4、设置重试次数
-t, --tries=NUMBER set number of retries to NUMBER (0 unlimits).--retry-connrefused retry even if connection is refused.
wget -t num可以设置当下载失败时重试的次数,0表示无限次重试,当网络环境不好的时候,可以设置重试次数,特别是下载大文件或者文件在国外网站时。
4、下载时指定下载下来的文件在本地的文件名
-O, --output-document=FILE write documents to FILE.
wget -O new_name url 将wget下载下来的文件重命名为新名称。
注:这里的wget参数-O是大写的字母'O'。
5、下载时跳过已存在的文件
-nc, --no-clobber skip downloads that would download toexisting files (overwriting them).
6、断点续传
-c, --continue resume getting a partially-downloaded file.--progress=TYPE select progress gauge type.
在下载大文件时,最好加上-c参数,避免下载的文件不完整。
7、文件有更新时下载
-N, --timestamping don't re-retrieve files unless newer thanlocal.
比较本地该文件时间戳跟服务器上该文件时间戳,只有当服务器该文件时间戳较新时,才下载该文件。
8、下载时显示服务器的回应包信息
-S, --server-response print server response.--spider don't download anything.
注:这里是大写字母S。
--spider 参数不实际下载文件,探测链接,能正常下载的话,会返回http会回应200:
9、设置超时时间
-T, --timeout=SECONDS set all timeout values to SECONDS.--dns-timeout=SECS set the DNS lookup timeout to SECS.--connect-timeout=SECS set the connect timeout to SECS.--read-timeout=SECS set the read timeout to SECS.
-T是设置所有的超时时间;
--dns-timeout=xxx 设置dns域名解析超时时间;
--connect-timeout=xxx 设置连接超时时间;
--read-timeout=xxx 设置读超时时间;
10、设置重试前等待时间
-w, --wait=SECONDS wait SECONDS between retrievals.--waitretry=SECONDS wait 1..SECONDS between retries of a retrieval.--random-wait wait from 0.5*WAIT...1.5*WAIT secs between retrievals.--no-proxy explicitly turn off proxy.
11、下载限速
--limit-rate=amount
限制下载速率(b/s,kb/s,mb/s),这里amount必须是十进制整数(不能用小数)
--limit-rate=512
--limit-rate=20k
--limit-rate=2m
三、设置下载目录
1、不根据url生成层级目录
-nd, --no-directories don't create directories.
递归下载时,不按url路径生成各级目录,将所有文件都放当前目录下,跟下面的-x命令功能刚好相反。
-nH, --no-host-directories don't create host directories.--protocol-directories use protocol name in directories.-P, --directory-prefix=PREFIX save files to PREFIX/...No options -> ftp.xemacs.org/pub/xemacs/
-nH -> pub/xemacs/
-nH --cut-dirs=1 -> xemacs/
-nH --cut-dirs=2 -> .
2、下载文件到根据url路径强制生成的目录下
-x, --force-directories force creation of directories.
注:这里x是小写字母。
3、下载文件到指定目录下
-P, --directory-prefix=PREFIX save files to PREFIX/...--cut-dirs=NUMBER ignore NUMBER remote directory components.
-P,大写的P,将下载的文件存放在-P参数指定的目录中:
四、http相关设置
1、设置http用户名密码
--http-user=USER set http user to USER.--http-password=PASS set http password to PASS.
2、禁用服务器cache缓存功能
--no-cache disallow server-cached data.
wget --no-cache url,下载不从服务器缓存中拿数据
3、其它扩展功能
-E, --adjust-extension save HTML/CSS documents with proper extensions.--ignore-length ignore `Content-Length' header field.--header=STRING insert STRING among the headers.--max-redirect maximum redirections allowed per page.--proxy-user=USER set USER as proxy username.--proxy-password=PASS set PASS as proxy password.--referer=URL include `Referer: URL' header in HTTP request.--save-headers save the HTTP headers to file.
--ignore-length 忽略头部Content-Length域;
--header=STRING 在http头部插入字符串;
--max-redirect 允许网页最大重定向次数;
--proxy-user=USER
--proxy-password=PASS 设置代理用户名及密码;
--referer=URL 在http头部插入Referer域;
--ignore-length 将http头保存到文件中;
五、用户代理相关设置
-U, --user-agent=AGENT identify as AGENT instead of Wget/VERSION.--no-http-keep-alive disable HTTP keep-alive (persistent connections).--no-cookies don't use cookies.--load-cookies=FILE load cookies from FILE before session.--save-cookies=FILE save cookies to FILE after session.--keep-session-cookies load and save session (non-permanent) cookies.--post-data=STRING use the POST method; send STRING as the data.--post-file=FILE use the POST method; send contents of FILE.--content-disposition honor the Content-Disposition header whenchoosing local file names (EXPERIMENTAL).--content-on-error output the received content on server errors.--auth-no-challenge send Basic HTTP authentication informationwithout first waiting for the server'schallenge.
六、https(tls/ssl)相关设置
HTTPS (SSL/TLS) options:--secure-protocol=PR choose secure protocol, one of auto, SSLv2,SSLv3, TLSv1, TLSv1_1 and TLSv1_2.--no-check-certificate don't validate the server's certificate.--certificate=FILE client certificate file.--certificate-type=TYPE client certificate type, PEM or DER.--private-key=FILE private key file.--private-key-type=TYPE private key type, PEM or DER.--ca-certificate=FILE file with the bundle of CA's.--ca-directory=DIR directory where hash list of CA's is stored.--random-file=FILE file with random data for seeding the SSL PRNG.--egd-file=FILE file naming the EGD socket with random data.
七、ftp相关设置
FTP options:--ftp-user=USER set ftp user to USER.--ftp-password=PASS set ftp password to PASS.--no-remove-listing don't remove `.listing' files.--no-glob turn off FTP file name globbing.--no-passive-ftp disable the "passive" transfer mode.--preserve-permissions preserve remote file permissions.--retr-symlinks when recursing, get linked-to files (not dir).
八、递归下载参数
1、递归下载url里的所有链接
-r, --recursive specify recursive download.
递归下载该网址里的所有链接
2、递归下载层级
-l, --level=NUMBER maximum recursion depth (inf or 0 for infinite).--delete-after delete files locally after downloading them.
-l num 设置最大递归到第几层目录
3、转换相对路径(链接)文件
-k, --convert-links make links in downloaded HTML or CSS point tolocal files.
-k 小写k,将HTML或者CSS中的连接文件指向本地文件
-p, --page-requisites get all images, etc. needed to display HTML page.--strict-comments turn on strict (SGML) handling of HTML comments.
下载所有图片等,可用于显示html网页,在下载网页用于脱机浏览时,一定要加这俩参数。
4、只下载/忽略下载指定类型文件
-A, --accept=LIST comma-separated list of accepted extensions.-R, --reject=LIST comma-separated list of rejected extensions.--accept-regex=REGEX regex matching accepted URLs.--reject-regex=REGEX regex matching rejected URLs.--regex-type=TYPE regex type (posix|pcre).
wget -A 后跟要下载的文件,比如要下载某个url下所有pdf、txt、jpg、zip、doc、mp4等格式文件,可以这样:
wget -A pdf,jpg,zip,doc,mp4,avi,rar,ppt,xls,tar.gz,*.txt download_url
wget -R 跟-A功能刚好相反,比如不下载html、php、asp、js、css等格式的文件:
wget -R html,htm,php,asp,jsp,js,py,css url
5、递归下载时不向上搜索到父目录
-np, --no-parent don't ascend to the parent directory.
参考:wget手册
Linux 上 wget 用法详解相关推荐
- linux signal用法,Linux 信号 signal 用法详解及注意事项
Linux 信号 signal 用法详解及注意事项 1) SIGHUP 本信号在用户终端连接 (正常或非正常) 结束时发出, 通常是在终端的控 制进程结束时, 通知同一 session 内的各个作业, ...
- linux中用less命令,Linux less 命令用法详解
原标题:Linux less 命令用法详解 less 是一个Linux命令行实用程序,用于显示文件或命令输出的内容,它一次只显示一个页面.它类似于 more ,但具有更高级的功能,允许您在文件中向前和 ...
- linux fork 用法,Linux系统调用fork()用法详解
linux 系统调用fork()的用法详解 Linux系统调用fork()用法详解 1. 先看下面代码: #include #include //pid_t类型定义 #include //函数fork ...
- linux下crontab命令的用法,linux 定时任务crontab用法详解
linux 定时任务crontab用法详解 linux中crontab命令用于设置周期性被执行的指令,该命令从标准输入设备读取指令,并将其存放于"crontab"文件中,以供之后读 ...
- Linux之wget命令详解
使用如下的命令下载https链接: wget -r -np -nd --accept=gz --no-check-certificate https://www.xxx.com/dir/ --http ...
- linux的vi命令详解,Linux上Vi命令详解
Linux下Vi命令详解 补充一点:在vi中使用命令的方法是:冒号+命令+回车,如:q 回车代表退出.================================================= ...
- linux下wget命令详解
linux下wget使用 wget是一个下载文件的工具,用在命令行下,下载一些软件或从远程服务器恢复备份到本地服务器:wget支持HTTP,HTTPS和FTP协议,可以使用HTTP代理 linux w ...
- jquery组件WebUploader文件上传用法详解
这篇文章主要为大家详细介绍了jquery组件WebUploader文件上传用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 WebUploader是由Baidu WebFE(FEX)团队开发的一 ...
- linux上ps命令详解
原文地址 linux上进程有5种状态: 1. 运行(正在运行或在运行队列中等待) 2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号) 3. 不可中断(收到信号不唤醒和不可运行, 进程必须 ...
最新文章
- 今天来个爪哇去边框的小代码
- html2canvas图片不能正常截取,html2canvas关于图片不能正常截取
- 揭秘《死者之书》之环境资源及特效创作
- C++多态的基本语法与原理剖析
- 第一站---大连---看海之旅
- $(document).ready(function(){}), $().ready(function(){}) 和 $(function(){}) 三个区别...
- spring mvc 总体概况
- 笨方法学python(本文为阅读时从此书摘录的笔记) 第四天
- 创建基于存储过程的数据块
- 从王者荣耀看设计模式(二.单例模式)
- 【LeetCode】【字符串】题号:*389. 找不同
- 揭秘马云帝国内幕:马云的野心有多大
- 拼多多电商外部工具(浏览器插件)
- Python爬取、可视化分析B站大司马视频40W+弹幕
- So easy!非技术人员也能学会的土狗防骗技巧!
- 说说 iphone 数据线(充电线) 原装正品和仿制品、假货等的区别
- 耶鲁大学计算机科学专业 录取,杜克大学和耶鲁大学计算机科学专业申请对比...
- 电脑桌面便签如何按农历日历设置便签提醒事项?
- 官方批复:同意博士研究生再翻倍扩招!
- 浅谈ERP实施应用的流程步骤