在一个 shell 脚本中,我想从某个 URL 下载一个文件并将其保存到指定的文件夹中。使用
curl
命令下载文件时,我应该使用哪个特定的 CLI 标志才能将文件下载到特定的文件夹中?如果不能使用该标志,还有其他方法可以实现这个目标吗?我认为你不能直接给curl一个路径,但你可以转到该位置,下载并返回。
cd target/path && { curl -O URL ; cd -; }
或者使用子shell。
(cd target/path && curl -O URL)
只有当路径存在时,两种方式才会下载。 -O
保留远程文件名。下载后,它将返回到原始位置。
如果需要显式设置文件名,可以使用小写的 -o
选项:
curl -o target/path/filename URL
basename /packages
"http://github.com/ziyaddin/xampp/archive/master.zip"。但它说文件名错误 --> basename /packages
。 - Ziyaddin Sadigov(cd 目标路径; curl -O URL)
。 - Ehtesh Choudhurycd
是最好的选择,尽管有点不太优雅。 - StockBfor url in $(cat urllistFile ) ; do name=$(basename $url) ; curl -o [yourDir]/$name $url ; done
- PaulRMcurl 7.73.0自从推出,就可以使用--output-dir
选项:
curl --create-dirs -O --output-dir /tmp/receipes https://example.com/pancakes.jpg
curl
7.68.0 存在 --create-dirs
但不存在 --output-dir
。 - Manuel Jordancurl
没有这个选项(不指定文件名的情况下),但 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
允许我设置想要放置的文件路径和命名以及扩展名。
使用重定向可以将 curl
下载的文件放置到指定路径中:
curl https://download.test.com/test.zip > /tmp/test.zip
显然,“test.zip”是您想要为重定向的文件标记的任意名称-可以是相同的名称或不同的名称。
实际上,我更喜欢@oderibas的解决方案,但在您的发行版支持curl版本7.73.0或更高版本之前,这将使您绕过该问题。
对于Windows中的PowerShell,您可以将相对路径和文件名添加到--output
标志中:
curl -L http://github.com/GorvGoyl/Notion-Boost-browser-extension/archive/master.zip --output build_firefox/master-repo.zip
这里的 build_firefox 是相对路径。
wget -P /your/absolut/path "https://jdbc.postgresql.org/download/postgresql-42.3.3.jar"
这里有一个使用批处理创建安全文件名并将其保存到名为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%
)
curl
是Invoke-WebRequest
cmdlet的别名,以下语法可用:curl "url" -OutFile file_name.ext
curl "https://airflow.apache.org/docs/apache-airflow/2.2.5/docker-compose.yaml" -OutFile docker-compose.yaml
来源:https://krypted.com/windows-server/its-not-wget-or-curl-its-iwr-in-windows/
pushd <path>; curl ...; popd;
- Sławomir Lenart