Save file to specific folder with curl command

BashShellCurlDirectory

Bash Problem Overview


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?

Bash Solutions


Solution 1 - Bash

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

Solution 2 - Bash

This option comes in curl 7.73.0:

curl --create-dirs -O --output-dir /tmp/receipes https://example.com/pancakes.jpg

Solution 3 - Bash

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"

Solution 4 - Bash

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.

Solution 5 - Bash

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.

Solution 6 - Bash

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-

Solution 7 - Bash

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 convert the 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.

Solution 8 - Bash

Use wget

wget -P /your/absolut/path "https://jdbc.postgresql.org/download/postgresql-42.3.3.jar"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZiyaddin SadigovView Question on Stackoverflow
Solution 1 - BashAtleView Answer on Stackoverflow
Solution 2 - BashoderibasView Answer on Stackoverflow
Solution 3 - BashwisbuckyView Answer on Stackoverflow
Solution 4 - BashJFAAView Answer on Stackoverflow
Solution 5 - BashGorvGoylView Answer on Stackoverflow
Solution 6 - BashF1LinuxView Answer on Stackoverflow
Solution 7 - BashLerieView Answer on Stackoverflow
Solution 8 - BashHrvojeView Answer on Stackoverflow