How to download a file into a directory using curl or wget?

LinuxMacosCurlWget

Linux Problem Overview


I know I can use the following 2 commands to download a file:

curl -O example.com/file.zip
wget example.com/file.zip

But I want them to go into a specific directory. So I can do the following:

curl -o mydir/file.zip example.com/file.zip
wget -O mydir/file.zip example.com/file.zip

Is there a way to not have to specify the filename? Something like this:

curl -dir mydir example.com/file.zip

Linux Solutions


Solution 1 - Linux

The following line will download all the files to a directory mentioned by you.

wget -P /home/test www.xyz.com

Here the files will be downloaded to /home/test directory

Solution 2 - Linux

I know this is an old question but it is possible to do what you ask with curl

rm directory/somefile.zip
rmdir directory
mkdir directory
curl --http1.1 http://example.com/somefile.zip --output directory/somefile.zip

first off if this was scripted you would need to make sure the file if it already exist is deleted then delete the directory then curl the download otherwise curl will fail with a file and directory already exist error.

Solution 3 - Linux

The simplest way is to cd inside a subshell

  (cd somedir; wget example.com/file.zip)

and you could make that a shell function (e.g. in your ~/.bashrc)

  wgetinside() {
    ( cd $1 ; shift; wget $* )
  }

then type wgetinside somedir http://example.com/file.zip

Solution 4 - Linux

Short answer is no as curl and wget automatically writes to STDOUT. It does not have an option built into to place the download file into a directory.

-o/--output <file> Write output to <file> instead of stdout (Curl)

-O,  --output-document=FILE    write documents to FILE. (WGet)

But as it outputs to STDOUT natively it does give you programatic solutions such as the following:

 i="YOURURL"; f=$(awk -F'/' '{print $NF}' <<< $i);curl $i > ~/$f

The first i will define your url (example.com/file.zip) as a variable. The f= part removed the URL and leaves /file.zip and then you curl that file ($i) to the directory (~) as the file name.

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
Questionat.View Question on Stackoverflow
Solution 1 - LinuxKishanView Answer on Stackoverflow
Solution 2 - LinuxmewasthereView Answer on Stackoverflow
Solution 3 - LinuxBasile StarynkevitchView Answer on Stackoverflow
Solution 4 - LinuxMattSizzleView Answer on Stackoverflow