【问题标题】:How to download a file into a directory using curl or wget? [closed]如何使用 curl 或 wget 将文件下载到目录中? [关闭]
【发布时间】:2013-10-25 08:23:04
【问题描述】:

我知道我可以使用以下 2 个命令来下载文件:

curl -O example.com/file.zip
wget example.com/file.zip

但我希望它们进入特定目录。所以我可以做到以下几点:

curl -o mydir/file.zip example.com/file.zip
wget -O mydir/file.zip example.com/file.zip

有没有办法不必指定文件名?像这样的:

curl -dir mydir example.com/file.zip

【问题讨论】:

    标签: linux macos curl wget


    【解决方案1】:

    以下行会将所有文件下载到您提到的目录中。

    wget -P /home/test www.xyz.com
    

    这里的文件将被下载到/home/test目录

    【讨论】:

    • 也为我工作!我还需要选项; --user 用户 --password 密码。
    • 如果您当前在一个目录中,请说 Documents 目录。使用路径 Documents/home/test,必须删除“home”之前的连字符才能使 wget 正常工作。
    • 我怎样才能对curl做同样的行为?
    【解决方案2】:

    我知道这是一个老问题,但可以用 curl 做你问的事情

    rm directory/somefile.zip
    rmdir directory
    mkdir directory
    curl --http1.1 http://example.com/somefile.zip --output directory/somefile.zip
    

    首先,如果这是脚本,你需要确保文件如果已经存在则被删除,然后删除目录,然后 curl 下载,否则 curl 将失败并出现文件和目录已存在错误。

    【讨论】:

    • 这正是问题中已经存在的内容。问题是选择一个目录但保留文件名。
    【解决方案3】:

    最简单的方法是在子shell中cd

      (cd somedir; wget example.com/file.zip)
    

    你可以把它变成一个shell函数(例如在你的~/.bashrc中)

      wgetinside() {
        ( cd $1 ; shift; wget $* )
      }
    

    然后输入wgetinside somedir http://example.com/file.zip

    【讨论】:

      【解决方案4】:

      简短的回答是否定的,因为 curl 和 wget 会自动写入 STDOUT。它没有内置选项将下载文件放入目录。

      -o/--output <file> Write output to <file> instead of stdout (Curl)
      
      -O,  --output-document=FILE    write documents to FILE. (WGet)
      

      但由于它以本机方式输出到 STDOUT,它确实为您提供了以下编程解决方案:

       i="YOURURL"; f=$(awk -F'/' '{print $NF}' <<< $i);curl $i > ~/$f
      

      首先,我将您的 url (example.com/file.zip) 定义为变量。 f= 部分删除了 URL 并留下 /file.zip,然后将该文件 ($i) 作为文件名卷曲到目录 (~) 中。

      【讨论】:

      • 您可以使用 basename 代替您的 awk 技巧。 basename 正在处理 URL(因为它使用最后一个 /
      • -o 的文档说:>> 该文件将保存在当前工作目录中。如果您希望文件保存在不同的目录中,请确保在使用此选项调用 curl 之前更改当前工作目录。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 2011-05-04
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 2015-10-04
      相关资源
      最近更新 更多