Linux 如何使用 curl 或 wget 将文件下载到目录中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19416662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to download a file into a directory using curl or wget?
提问by at.
I know I can use the following 2 commands to download a file:
我知道我可以使用以下 2 个命令来下载文件:
curl -O example.com/file.zip
wget example.com/file.zip
But I want them to go into a specific directory. So I can do the following:
但我希望他们进入一个特定的目录。所以我可以执行以下操作:
curl -o mydir/file.zip example.com/file.zip
wget -O mydir/file.zip example.com/file.zip
Is there a way to not have to specify the filename? Something like this:
有没有办法不必指定文件名?像这样的东西:
curl -dir mydir example.com/file.zip
采纳答案by Kishan
The following line will download all the files to a directory mentioned by you.
以下行会将所有文件下载到您提到的目录中。
wget -P /home/test www.xyz.com
Here the files will be downloaded to /home/test
directory
这里的文件将被下载到/home/test
目录
回答by MattSizzle
Short answer is no as curl and wget automatically writes to STDOUT. It does not have an option built into to place the download file into a directory.
简短的回答是否定的,因为 curl 和 wget 会自动写入 STDOUT。它没有内置选项将下载文件放入目录。
-o/--output <file> Write output to <file> instead of stdout (Curl)
-O, --output-document=FILE write documents to FILE. (WGet)
But as it outputs to STDOUT natively it does give you programatic solutions such as the following:
但是当它本机输出到 STDOUT 时,它确实为您提供了编程解决方案,例如:
i="YOURURL"; f=$(awk -F'/' '{print $NF}' <<< $i);curl $i > ~/$f
The first i will define your url (example.com/file.zip) as a variable. The f= part removed the URL and leaves /file.zip and then you curl that file ($i) to the directory (~) as the file name.
第一个我将你的 url (example.com/file.zip) 定义为一个变量。f= 部分删除了 URL 并留下 /file.zip,然后将该文件 ($i) 卷曲到目录 (~) 作为文件名。
回答by Basile Starynkevitch
The simplest way is to cd
inside a subshell
最简单的方法是cd
在子shell中
(cd somedir; wget example.com/file.zip)
and you could make that a shell function (e.g. in your ~/.bashrc
)
你可以把它变成一个 shell 函数(例如在你的~/.bashrc
)
wgetinside() {
( cd ; shift; wget $* )
}
then type wgetinside somedir http://example.com/file.zip
然后输入 wgetinside somedir http://example.com/file.zip
回答by mewasthere
I know this is an old question but it is possible to do what you ask with curl
我知道这是一个老问题,但可以用 curl 做你问的事情
rm directory/somefile.zip
rmdir directory
mkdir directory
curl --http1.1 http://example.com/somefile.zip --output directory/somefile.zip
first off if this was scripted you would need to make sure the file if it already exist is deleted then delete the directory then curl the download otherwise curl will fail with a file and directory already exist error.
首先,如果这是脚本化的,您需要确保已存在的文件被删除,然后删除目录,然后 curl 下载,否则 curl 将失败并显示文件和目录已存在错误。