阅读目录:详解wget命令-文件下载工具
wget是Linux系统中的一个下载文件的工具,用在命令行下。用于下载一些软件或从远程服务器恢复备份到本地服务器。(不能下载目录)
wget的优点:
wget 非常稳定,在带宽很窄的情况下和不稳定网络中有很强的适应性:
①如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕。
②如果是服务器打断下载过程,它会再次联到服务器上从停止的地方继续下载。
③wget支持HTTP,HTTPS和FTP协议,可以使用HTTP代理。
自动下载:wget可以在用户退出系统的之后在后台执行(=可以登录系统,启动一个wget下载任务,然后退出系统,wget将在后台执行直到任务完成)
递归下载:wget 可以跟踪HTML页面上的链接依次下载来创建远程服务器的本地版本,完全重建原始站点的目录结构。
1 2 3 4 5 |
<span style="font-size: 16px;"># tar zxvf wget-<span style="color: rgba(128, 0, 128, 1);">1.9</span>.<span style="color: rgba(128, 0, 128, 1);">1</span><span style="color: rgba(0, 0, 0, 1);">.tar.gz # cd wget</span>-<span style="color: rgba(128, 0, 128, 1);">1.9</span>.<span style="color: rgba(128, 0, 128, 1);">1</span><span style="color: rgba(0, 0, 0, 1);"> # .</span>/</span><span style="color: rgba(0, 0, 0, 1);"><span style="font-size: 16px;">configure # make # make install</span> </span> |
wget [参数] [URL地址]
用于从网络上下载资源,没有指定目录,默认下载资源到当前目录
①支持断点下传功能;这一点,也是网络蚂蚁和FlashGet当年最大的卖点,现在,Wget也可以使用此功能,那些网络不是太好的用户可以放心了;
②同时支持FTP和HTTP下载方式;尽管现在大部分软件可以使用HTTP方式下载,但是,有些时候,仍然需要使用FTP方式下载软件;wget ftp://172.16.1.1/access.log
③支持代理服务器;对安全强度很高的系统而言,一般不会将自己的系统直接暴露在互联网上,所以,支持代理是下载软件必须有的功能;
④设置方便简单;可能,习惯图形界面的用户已经不是太习惯命令行了,但是,命令行在设置上其实有更多的优点,最少,鼠标可以少点很多次,也不要担心是否错点鼠标;
⑤程序小,完全免费;程序小可以考虑不计,因为现在的硬盘实在太大了;完全免费就不得不考虑了,即使网络上有很多所谓的免费软件,但是,这些软件的广告却不是我们喜欢的。
1 2 3 4 |
<span style="font-size: 16px;">-V, –version <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">显示wget的版本后退出</span> -h, –help <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">打印语法帮助</span> -b, –background <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">启动后转入后台执行</span> -e, –execute=COMMAND <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">执行`.wgetrc’格式的命令,wgetrc格式参见/etc/wgetrc或~/.wgetrc</span></span> |
1 2 3 4 5 6 7 8 9 10 11 12 |
<span style="font-size: 16px;">-o, –output-file=FILE <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">把记录写到FILE文件中</span> -a, –append-output=FILE <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">把记录追加到FILE文件中</span> -d, –debug <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">打印调试输出</span> -q, –quiet <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">安静模式(没有输出)</span> -v, –verbose <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">冗长模式(这是缺省设置)</span> -nv, –non-verbose <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">关掉冗长模式,但不是安静模式</span> -i, –input-file=FILE <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">下载在FILE文件中出现的URLs</span> -F, –force-html <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">把输入文件当作HTML格式文件对待</span> -B, –<span style="color: rgba(0, 0, 255, 1);">base</span>=URL <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">将URL作为在-F -i参数指定的文件中出现的相对链接的前缀</span><span style="color: rgba(0, 0, 0, 1);"> –sslcertfile</span>=FILE <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">可选客户端证书</span><span style="color: rgba(0, 0, 0, 1);"> –sslcertkey</span>=KEYFILE <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">可选客户端证书的KEYFILE</span><span style="color: rgba(0, 0, 0, 1);"> –egd</span>-file=FILE <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">指定EGD socket的文件名</span></span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span style="font-size: 16px;">–bind-address=ADDRESS <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">指定本地使用地址(主机名或IP,当本地有多个IP或名字时使用)</span> -t, –tries=NUMBER <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">设定最大尝试链接次数(0 表示无限制).</span> -O –output-document=FILE <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">把文档写到FILE文件中</span> -nc, –no-clobber <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">不要覆盖存在的文件或使用.#前缀</span> -c, –<span style="color: rgba(0, 0, 255, 1);">continue</span> <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">接着下载没下载完的文件</span><span style="color: rgba(0, 0, 0, 1);"> –progress</span>=TYPE <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">设定进程条标记</span> -N, –timestamping <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">不要重新下载文件除非比本地文件新</span> -S, –server-response <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">打印服务器的回应</span><span style="color: rgba(0, 0, 0, 1);"> –spider </span><span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">不下载任何东西</span> -T, –timeout=SECONDS <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">设定响应超时的秒数</span> -w, –wait=SECONDS <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">两次尝试之间间隔SECONDS秒</span><span style="color: rgba(0, 0, 0, 1);"> –waitretry</span>=SECONDS <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">在重新链接之间等待1…SECONDS秒</span><span style="color: rgba(0, 0, 0, 1);"> –random</span>-wait <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">在下载之间等待0…2*WAIT秒</span> -Y, –proxy=on/off <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">打开或关闭代理</span> -Q, –quota=NUMBER <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">设置下载的容量限制</span><span style="color: rgba(0, 0, 0, 1);"> –limit</span>-rate=RATE <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">限定下载输率</span></span> |
1 2 3 4 5 |
<span style="font-size: 16px;">-nd –no-directories <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">不创建目录</span> -x, –force-directories <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">强制创建目录</span> -nH, –no-host-directories <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">不创建主机目录</span> -P, –directory-prefix=PREFIX <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">将文件保存到目录 PREFIX/…</span><span style="color: rgba(0, 0, 0, 1);"> –cut</span>-dirs=NUMBER <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">忽略 NUMBER层远程目录</span></span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="font-size: 16px;">–http-user=USER <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">设定HTTP用户名为 USER.</span><span style="color: rgba(0, 0, 0, 1);"> –http</span>-passwd=PASS <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">设定http密码为 PASS</span> -C, –cache=on/off <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">允许/不允许服务器端的数据缓存 (一般情况下允许)</span> -E, –html-extension <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">将所有text/html文档以.html扩展名保存</span><span style="color: rgba(0, 0, 0, 1);"> –ignore</span>-length <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">忽略 `Content-Length’头域</span><span style="color: rgba(0, 0, 0, 1);"> –header</span>=STRING <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">在headers中插入字符串 STRING</span><span style="color: rgba(0, 0, 0, 1);"> –proxy</span>-user=USER <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">设定代理的用户名为 USER</span><span style="color: rgba(0, 0, 0, 1);"> –proxy</span>-passwd=PASS <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">设定代理的密码为 PASS</span><span style="color: rgba(0, 0, 0, 1);"> –referer</span>=URL <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">在HTTP请求中包含 `Referer: URL’头</span> -s, –save-headers <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">保存HTTP头到文件</span> -U, –user-agent=AGENT <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">设定代理的名称为 AGENT而不是 Wget/VERSION</span><span style="color: rgba(0, 0, 0, 1);"> –no</span>-http-keep-alive <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">关闭 HTTP活动链接 (永远链接)</span><span style="color: rgba(0, 0, 0, 1);"> –cookies</span>=off <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">不使用 cookies</span><span style="color: rgba(0, 0, 0, 1);"> –load</span>-cookies=FILE <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">在开始会话前从文件 FILE中加载cookie</span><span style="color: rgba(0, 0, 0, 1);"> –save</span>-cookies=FILE <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">在会话结束后将 cookies保存到 FILE文件中</span></span> |
1 2 3 4 5 |
<span style="font-size: 16px;">-nr, –dont-remove-listing <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">不移走 `.listing’文件</span> -g, –glob=on/off <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">打开或关闭文件名的 globbing机制</span><span style="color: rgba(0, 0, 0, 1);"> –passive</span>-ftp <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">使用被动传输模式 (缺省值).</span><span style="color: rgba(0, 0, 0, 1);"> –active</span>-ftp <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">使用主动传输模式</span><span style="color: rgba(0, 0, 0, 1);"> –retr</span>-symlinks <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">在递归的时候,将链接指向文件(而不是目录)</span></span> |
1 2 3 4 5 6 7 |
<span style="font-size: 16px;">-r, –recursive <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">递归下载--慎用!</span> -l, –level=NUMBER <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">最大递归深度 (inf 或 0 代表无穷)</span><span style="color: rgba(0, 0, 0, 1);"> –delete</span>-after <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">在现在完毕后局部删除文件</span> -k, –convert-links <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">转换非相对链接为相对链接</span> -K, –backup-converted <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">在转换文件X之前,将之备份为 X.orig</span> -m, –mirror <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">等价于 -r -N -l inf -nr</span> -p, –page-requisites <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">下载显示HTML文件的所有图片</span></span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span style="font-size: 16px;">-A, –accept=LIST <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">分号分隔的被接受扩展名的列表</span> -R, –reject=LIST <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">分号分隔的不被接受的扩展名的列表</span> -D, –domains=LIST <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">分号分隔的被接受域的列表</span><span style="color: rgba(0, 0, 0, 1);"> –exclude</span>-domains=LIST <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">分号分隔的不被接受的域的列表</span><span style="color: rgba(0, 0, 0, 1);"> –follow</span>-ftp <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">跟踪HTML文档中的FTP链接</span><span style="color: rgba(0, 0, 0, 1);"> –follow</span>-tags=LIST <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">分号分隔的被跟踪的HTML标签的列表</span> -G, –ignore-tags=LIST <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">分号分隔的被忽略的HTML标签的列表</span> -H, –span-hosts <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">当递归时转到外部主机</span> -L, –relative <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">仅仅跟踪相对链接</span> -I, –include-directories=LIST <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">允许目录的列表</span> -X, –exclude-directories=LIST <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">不被包含目录的列表</span> -np, –no-parent <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">不要追溯到父目录</span><span style="color: rgba(0, 0, 0, 1);"> wget </span>-S –spider url <span style="color: rgba(0, 128, 0, 1);">//</span><span style="color: rgba(0, 128, 0, 1);">不下载只显示过程</span></span> |
实例1:使用wget下载单个文件
命令:
wget http://www.minjieren.com/wordpress-3.1-zh_CN.zip
说明:
以下的例子是从网络下载一个文件并保存在当前目录,在下载的过程中会显示进度条,包含(下载完成百分比,已经下载的字节,当前下载速度,剩余下载时间)。
实例2:使用wget -O下载并以不同的文件名保存
命令:
wget -O wordpress.zip http://www.minjieren.com/download.aspx?id=1080
说明:
wget默认会以最后一个符合”/”的后面的字符来命名,对于动态链接的下载通常文件名会不正确。若不接命令参数,(如上)就是下载一个文件并以名称download.aspx?id=1080保存,使用参数-O来指定一个文件名wordpress.zip。
实例3:使用wget –limit -rate限速下载
命令:
wget –limit-rate=300k http://www.minjieren.com/wordpress-3.1-zh_CN.zip
说明:
当你执行wget的时候,默认会占用全部可能的宽带下载。–limit-rate=300k进行必要限速。
实例4:使用wget -c断点续传
命令:
wget -c http://www.minjieren.com/wordpress-3.1-zh_CN.zip
说明:
使用wget -c重新启动下载由于网络等原因中断文件,继续接着下载而不是重新下载一个文件。
实例5:使用wget -b后台下载
命令:
wget -b http://www.minjieren.com/wordpress-3.1-zh_CN.zip
说明:
对于下载非常大的文件的时候,使用参数-b进行后台下载。
wget -b http://www.minjieren.com/wordpress-3.1-zh_CN.zip
Continuing in background, pid 1840.
Output will be written to `wget-log’.
查看下载进度:tail -f wget-log
实例6:伪装代理名称下载
命令:
wget –user-agent=”Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16″ http://www.minjieren.com/wordpress-3.1-zh_CN.zip
说明:
通过–user-agent参数进行伪装下载(有些网站能通过根据判断代理名称不是浏览器而拒绝你的下载请求)
实例7:使用wget –spider测试下载链接
命令:
wget –spider URL
说明:
若是打算进行定时下载,应该在预定时间测试下载链接是否有效。–spider参数进行检查。
shell> wget –spider URL #如果下载链接正确
Spider mode enabled. Check if remote file exists.
HTTP request sent, awaiting response… 200 OK
Length: unspecified [text/html]
Remote file exists and could contain further links,
but recursion is disabled — not retrieving.
shell> wget –spider URL #如果下载链接错误
Spider mode enabled. Check if remote file exists.
HTTP request sent, awaiting response… 404 Not Found
Remote file does not exist — broken link!!!
几种情况下使用spider参数:定时下载之前进行检查、间隔检测网站是否可用、检查网站页面的死链接
实例8:使用wget –tries增加重试次数
命令:
wget –tries=40 URL
说明:
网络有问题或下载一个大文件都有可能失败(wget默认重试20次连接下载文件)
实例9:使用wget -i下载多个文件
命令:
wget -i filelist.txt
说明:
首先,保存一份下载链接文件,然后使用这个文件和参数-i下载
shell> cat filelist.txt
url1
url2
url3
url4
实例10:使用wget –mirror镜像网站
命令:
wget –mirror -p –convert-links -P ./LOCAL URL
说明:
下载整个网站到本地
–miror:开户镜像下载
-p:下载所有为了html页面显示正常的文件
–convert-links:下载后,转换成本地的链接
-P ./LOCAL:保存所有文件和目录到本地指定目录
实例11:使用wget –reject过滤指定格式下载
命令:
wget –reject=gif ur
说明:
下载一个网站,但过滤gif格式=不下载图片。
实例12:使用wget -o把下载信息存入日志文件
命令:
wget -o download.log URL
说明:
将下载信息output到一个日志文件,而不是直接显示在终端。
实例13:使用wget -Q限制总下载文件大小
命令:
wget -Q5m -i filelist.txt
说明:
下载的文件超过5M而退出下载
注意:这个参数对单个文件下载不起作用,只能递归下载时才有效。
实例14:使用wget -r -A下载指定格式文件
命令:
wget -r -A.pdf url
说明:
可以在以下情况使用该功能:
下载一个网站的所有图片
下载一个网站的所有视频
下载一个网站的所有PDF文件
实例15:使用wget FTP下载(ftp链接的下载)
命令:
wget ftp-url
wget –ftp-user=USERNAME –ftp-password=PASSWORD url
说明:
使用wget匿名ftp下载:wget ftp-url
使用wget用户名和密码认证的ftp下载:wget –ftp-user=USERNAME –ftp-password=PASSWORD url