Save file to specific folder with curl command

332,569

Solution 1

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

This option comes in curl 7.73.0:

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

Solution 3

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

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

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.

Share:
332,569

Related videos on Youtube

Ziyaddin Sadigov
Author by

Ziyaddin Sadigov

Updated on April 11, 2022

Comments

  • Ziyaddin Sadigov
    Ziyaddin Sadigov 9 months

    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?

    • Tom N Tech
      Tom N Tech about 3 years
      It would seem that bash programming is programming...
  • Ziyaddin Sadigov
    Ziyaddin Sadigov over 9 years
    I have this command: curl -LOk basename /packages "github.com/ziyaddin/xampp/archive/master.zip". But it says that wrong filename --> basename /packages
  • Ehtesh Choudhury
    Ehtesh Choudhury over 8 years
    You can also use a subshell, like so: (cd target/path; curl -O URL)
  • hzhu
    hzhu almost 8 years
    What 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.
  • turtlemonvh
    turtlemonvh over 7 years
    @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.
  • StockB
    StockB over 5 years
    I 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.
  • PaulRM
    PaulRM over 3 years
    If 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
  • Zorono
    Zorono about 2 years
    unfortunately, those options aren't available for Ubuntu since version 7.73.0 is not intended yet for ubuntu :(
  • F1Linux
    F1Linux 9 months
    Although 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
  • Manuel Jordan
    Manuel Jordan 8 months
    On Ubuntu Server 20.04 - for curl 7.68.0 exists --create-dirs but does not exist --output-dir