263

在 shell 脚本中,我想从某个 URL 下载文件并将其保存到特定文件夹。我应该使用什么特定的 CLI 标志来使用命令将文件下载到特定文件夹curl,或者我如何获得该结果?

4

6 回答 6

381

I don't think you can give a path to curl, but you can CD to the location, download and CD back.

cd target/path && { curl -O URL ; cd -; }

Or using subshell.

(cd target/path && curl -O URL)

Both ways will only download if path exists. -O keeps remote file name. After download it will return to original location.

If you need to set filename explicitly, you can use small -o option:

curl -o target/path/filename URL
于 2013-05-03T16:00:40.517 回答
44

此选项出现在 curl 7.73.0 中

curl --create-dirs -O --output-dir /tmp/receipes https://example.com/pancakes.jpg
于 2020-09-11T08:23:47.037 回答
23

curl没有选项(也没有指定文件名),但是wget有。该目录可以是相对的或绝对的。此外,如果目录不存在,将自动创建该目录。

wget -P relative/dir "$url"

wget -P /absolute/dir "$url"
于 2020-05-12T06:14:02.300 回答
4

这个对我有用:

curl http://centos.mirror.constant.com/8-stream/isos/aarch64/CentOS-Stream-8-aarch64-20210916-boot.iso --output ~/Downloads/centos.iso 

在哪里:

--output允许我设置我想要放置的文件和扩展文件的路径和命名。

于 2021-09-27T15:53:18.187 回答
3

对于 Windows 中的 powershell,您可以添加相对路径+文件名--output标记:

curl -L  http://github.com/GorvGoyl/Notion-Boost-browser-extension/archive/master.zip --output build_firefox/master-repo.zip

这里build_firefox是相对文件夹。

于 2020-11-11T12:36:17.720 回答
0

这是一个使用 Batch 从 URL 创建安全文件名并将其保存到名为tmp/的文件夹的示例。我确实认为这不是 Windows 或 Linux Curl 版本的选项很奇怪。

@echo off
set url=%1%

for /r %%f in (%url%) do (
    set url=%%~nxf.txt
    curl --create-dirs -L -v -o tmp/%%~nxf.txt %url%
)

上面的批处理文件将采用单个输入,一个 URL,并从 url 转换文件名。如果没有指定文件名,它将被保存为tmp/.txt。因此,它并没有为您完成所有工作,但它可以在 Windows 中完成工作。

于 2022-02-02T16:32:16.810 回答