bash 使用 curl 命令将文件保存到特定文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16362402/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:39:51  来源:igfitidea点击:

Save file to specific folder with curl command

bashshellcurldirectory

提问by Ziyaddin Sadigov

In a shell script, I want to download a file from some URL and save it to a specific folder. What is the specific CLI flag I should use to download files to a specific folder with the curlcommand, or how else do I get that result?

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

回答by Atle

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

我不认为您可以提供 curl 的路径,但是您可以 CD 到该位置,下载并 CD 回来。

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

Or using subshell.

或者使用子shell。

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

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

两种方式都只会在路径存在时下载。-O保留远程文件名。下载后会回到原来的位置。

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

如果需要明确设置文件名,可以使用小-o选项:

curl -o target/path/filename URL

回答by wisbucky

curldoesn't have an option to that (without also specifying the filename), but wgetdoes. The directory can be relative or absolute. Also, the directory will automatically be created if it doesn't exist.

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

wget -P relative/dir "$url"

wget -P /absolute/dir "$url"