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 curl
command, or how else do I get that result?

- 8,893
- 13
- 34
- 41
-
simply wrap a curl with 2 extra commands: `pushd
; curl ...; popd;` – Sławomir Lenart Feb 25 '23 at 16:49
9 Answers
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

- 19,847
- 9
- 124
- 140

- 5,299
- 1
- 15
- 10
-
2I have this command: curl -LOk `basename /packages` "http://github.com/ziyaddin/xampp/archive/master.zip". But it says that wrong filename --> `basename /packages` – Ziyaddin Sadigov May 03 '13 at 16:24
-
13You can also use a subshell, like so: `(cd target/path; curl -O URL)` – Ehtesh Choudhury May 13 '14 at 18:03
-
10What is the difference between the two? The first one cd's into a the directory and downloads the file, then cds out. The second stays in current directory and curls file to specificed location. Second one seems more simple. – hzhu Mar 08 '15 at 06:46
-
10@HenryZhu In the first one the filename is derived from the name of the file on the server. In the second you are renaming the content you downloaded to a name you provide. – turtlemonvh Aug 20 '15 at 00:14
-
4I would really like an option to specify a directory, but use the server's filename. It seems like using `cd` is the best option currently, though it seems slightly inelegant. – StockB Jun 26 '17 at 15:56
-
2If you are in a script, you can do something like: `for url in $(cat urllistFile ) ; do name=$(basename $url) ; curl -o [yourDir]/$name $url ; done` – PaulRM Jul 19 '19 at 03:31
The --output-dir
option is available since curl 7.73.0:
curl --create-dirs -O --output-dir /tmp/receipes https://example.com/pancakes.jpg

- 15,253
- 21
- 95
- 158

- 1,575
- 12
- 20
-
12unfortunately, those options aren't available for Ubuntu since version 7.73.0 is not intended yet for ubuntu :( – Zorono Nov 21 '20 at 11:50
-
2Although still doesn't work on Ubuntu- curl version is sadly 7.68.0 for 20.04 LTS- I can confirm it works on OSX Monterey, so worthy of an upvote – F1Linux Apr 10 '22 at 08:49
-
On Ubuntu Server 20.04 - for `curl` _7.68.0_ exists `--create-dirs` but does **not** exist `--output-dir` – Manuel Jordan Apr 16 '22 at 14:47
-
-
4
curl
doesn't have an option to that (without also specifying the filename), but wget
does. The directory can be relative or absolute. Also, the directory will automatically be created if it doesn't exist.
wget -P relative/dir "$url"
wget -P /absolute/dir "$url"

- 33,218
- 10
- 150
- 101
it works for me:
curl http://centos.mirror.constant.com/8-stream/isos/aarch64/CentOS-Stream-8-aarch64-20210916-boot.iso --output ~/Downloads/centos.iso
where:
--output
allows me to set up the path and the naming of the file and extension file that I want to place.

- 4,555
- 31
- 31
- 45

- 113
- 1
- 6
Use redirection:
This works to drop a curl
downloaded file into a specified path:
curl https://download.test.com/test.zip > /tmp/test.zip
Obviously "test.zip" is whatever arbitrary name you want to label the redirected file- could be the same name or a different name.
I actually prefer @oderibas solution, but this will get you around the issue until your distro supports curl version 7.73.0 or later-

- 3,580
- 3
- 25
- 24
For powershell in Windows, you can add relative path + filename to --output
flag:
curl -L http://github.com/GorvGoyl/Notion-Boost-browser-extension/archive/master.zip --output build_firefox/master-repo.zip
here build_firefox is relative folder.

- 42,508
- 29
- 229
- 225
Use wget
wget -P /your/absolut/path "https://jdbc.postgresql.org/download/postgresql-42.3.3.jar"

- 13,566
- 7
- 90
- 104
Here is an example using Batch to create a safe filename from a URL and save it to a folder named tmp/. I do think it's strange that this isn't an option on the Windows or Linux Curl versions.
@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%
)
The above Batch file will take a single input, a URL, and create a filename from the url. If no filename is specified it will be saved as tmp/.txt. So it's not all done for you but it gets the job done in Windows.

- 26
- 6
For Windows, in PowerShell, curl
is an alias of the cmdlet Invoke-WebRequest
and this syntax works:
curl "url" -OutFile file_name.ext
For instance:
curl "https://airflow.apache.org/docs/apache-airflow/2.2.5/docker-compose.yaml" -OutFile docker-compose.yaml
Source: https://krypted.com/windows-server/its-not-wget-or-curl-its-iwr-in-windows/

- 1,663
- 2
- 10
- 10

- 17
- 6