首页 文章

如何用wget重命名下载的文件?

提问于
浏览
58

要从服务器下载SOFA Statistics,我使用wget命令:

wget -c http://sourceforge.net/projects/sofastatistics/files/latest/download?source=dlp

在这种情况下,下载文件的文件名是 download?source=files . 如果我在命令中添加 --output-document 选项,要将输出文件重命名为 sofastatistics-latest.deb ,dpkg软件包无法识别下载文件的格式 .

dpkg-deb: error: `sofastatistics-latest.deb' is not a debian format archive

如何使用wget正确重命名下载的文件?

UPDATE - Jan 08 '15

使用提供的链接,下载的文件将始终为* .tar.gz . 要使用真实名称,只需添加 --content-disposition 选项(感谢@ 6EQUJ5!):

wget --content-disposition http://sourceforge.net/projects/sofastatistics/files/latest/download?source=dlp

但我需要一个* .deb文件,所以这里是@creaktive,我必须搜索* .deb文件链接 .

感谢大家的答案!

3 回答

  • 1

    将标准输出重定向到任意文件名始终有效 . 正如 man wget 所说,使用-O正确地执行此操作

    wget http://www.kernel.org/pub/linux/kernel/README -O foo
    --2013-01-13 18:59:44--  http://www.kernel.org/pub/linux/kernel/README
    Resolving www.kernel.org... 149.20.4.69, 149.20.20.133
    Connecting to www.kernel.org|149.20.4.69|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 12056 (12K) [text/plain]
    Saving to: `foo'
    
    100%[======================================================================================================================================>] 12,056      --.-K/s   in 0.003s  
    
    2013-01-13 18:59:45 (4.39 MB/s) - `foo' saved [12056/12056]
    

    实际上,您必须在文件中获取HTML(通常可以使用 man file 进行检查) .

    [EDIT]

    在您的情况下,客户端正在接收302 Found(您可以使用 curl -v URL 进行检查) .

    以下curl通过尊重3xx来做到这一点:

    $ curl -L http://sourceforge.net/projects/sofastatistics/files/latest/download?source=files -o foo.deb
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
      0   463    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
      0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
    100 2035k  100 2035k    0     0   390k      0  0:00:05  0:00:05 --:--:-- 1541k
    $ file foo.deb 
    foo.deb: gzip compressed data, was "sofastats-1.3.1.tar", last modified: Thu Jan 10 00:30:44 2013, max compression
    

    wget 应该有类似的选项来容忍HTTP重定向 .

  • 17

    如果您要从Web浏览器执行相同的下载,并且您注意到浏览器实际上正确地命名了文件,则可以使用 --content-disposition 选项为wget提供相同的行为:

    wget --content-disposition http://sourceforge.net/projects/sofastatistics/files/latest/download?source=dlp
    

    我的Debian手册页将此报告为“实验性”功能,但我不记得它不适合我:

    --content-disposition
               If this is set to on, experimental (not fully-functional) support for "Content-Disposition" headers is enabled. This can currently result in extra round-trips to the server
               for a "HEAD" request, and is known to suffer from a few bugs, which is why it is not currently enabled by default.
    
               This option is useful for some file-downloading CGI programs that use "Content-Disposition" headers to describe what the name of a downloaded file should be.
    
  • 84

    该链接指向重定向器,而不是最终目标!因此,您正在下载HTML并将其重命名为 .deb . 杂乱的页面围绕顶部:

    您的下载将在0秒后开始...下载问题?请使用此直接链接,或尝试其他镜像 .

    现在,这是一个有效的链接(注意 download 前缀):http://downloads.sourceforge.net/project/sofastatistics/sofastatistics/1.3.1/sofastats-1.3.1-1_all.deb?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fsofastatistics%2Ffiles%2Fsofastatistics%2F1.3.1%2F&ts=1358119361&use_mirror=ufpr

    将此网址传递给 wget . 另请注意,SourceForge会尝试通过用户代理字符串来操作操作系统,从而超越您 . "wget"的最佳猜测似乎是.tar.gz包 . 所以,你应该更具体,请求deb文件!

相关问题