在 shell 脚本中,我想从某个 URL 下载文件并将其保存到特定文件夹。我应该使用什么特定的 CLI 标志来使用命令将文件下载到特定文件夹curl
,或者我如何获得该结果?
6 回答
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
curl --create-dirs -O --output-dir /tmp/receipes https://example.com/pancakes.jpg
curl
没有选项(也没有指定文件名),但是wget
有。该目录可以是相对的或绝对的。此外,如果目录不存在,将自动创建该目录。
wget -P relative/dir "$url"
wget -P /absolute/dir "$url"
这个对我有用:
curl http://centos.mirror.constant.com/8-stream/isos/aarch64/CentOS-Stream-8-aarch64-20210916-boot.iso --output ~/Downloads/centos.iso
在哪里:
--output
允许我设置我想要放置的文件和扩展文件的路径和命名。
对于 Windows 中的 powershell,您可以添加相对路径+文件名来--output
标记:
curl -L http://github.com/GorvGoyl/Notion-Boost-browser-extension/archive/master.zip --output build_firefox/master-repo.zip
这里build_firefox是相对文件夹。
这是一个使用 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 中完成工作。