57

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
at.
  • 50,922
  • 104
  • 292
  • 461

4 Answers4

57

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

Vy Do
  • 46,709
  • 59
  • 215
  • 313
Kishan
  • 49
  • 3
  • 11
17

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.

mewasthere
  • 179
  • 1
  • 2
  • 1
    This is exactly what was aleeady in the question. The question was for selecting a directoriy but keeping the file name. – Jonas Eberle Oct 24 '20 at 13:23
7

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

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
3

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.

MattSizzle
  • 3,145
  • 1
  • 22
  • 42
  • 1
    You could use `basename` instead of your `awk` trick. `basename` is working on URLs (because it uses the last `/`) – Basile Starynkevitch Oct 17 '13 at 05:06
  • Documentation for -o says: >> The file will be saved in the current working directory. If you want the file saved in a different directory, make sure you change the current working directory before invoking curl with this option. – Meglio Dec 01 '17 at 11:03