905

I need files to be downloaded to /tmp/cron_test/. My wget code is

wget --random-wait -r -p -nd -e robots=off -A".pdf" -U mozilla http://math.stanford.edu/undergrad/

So is there some parameter to specify the directory?

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • 2
    Similar at Unix SE: [Download using wget to a different directory than current directory](http://unix.stackexchange.com/q/23501/21471). – kenorb Aug 11 '15 at 23:47
  • 1
    There's a good chance people may want to use `-x` if they are using `-i` or `--input-files`, to force it to download to the corresponding local directory that matches each URL pattern. – Andrew Jan 17 '21 at 02:57

5 Answers5

1187

From the manual page:

-P prefix
--directory-prefix=prefix
           Set directory prefix to prefix.  The directory prefix is the
           directory where all other files and sub-directories will be
           saved to, i.e. the top of the retrieval tree.  The default
           is . (the current directory).

So you need to add -P /tmp/cron_test/ (short form) or --directory-prefix=/tmp/cron_test/ (long form) to your command. Also note that if the directory does not exist it will get created.

allan.simon
  • 3,886
  • 6
  • 35
  • 60
RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • 8
    -P /tmp/cron_test/ doesnt work but removing / like -P tmp/cron_test/ works and even creates nonexisting directory. – Kangarooo Oct 25 '12 at 00:11
  • 12
    Kangaroo it probably doesnt work because you dont have /tmp/cron_test/ on your system :P – Sameer Alibhai Sep 29 '13 at 14:22
  • 73
    The manual's description makes this option hard to search for. I don't think of the location where I want to save something as a 'directory prefix'. Thanks for sharing! – Iain Samuel McLean Elder Mar 06 '14 at 23:03
  • 8
    Also, you can remove the root folder via `--no-host-directories` or `-nH` as per http://serverfault.com/questions/354792/downloading-from-wget-folder-issue – Alien Life Form Jul 01 '15 at 03:43
  • 1
    -P is ignored if using an input file with -i any ideas? – Jan Viehweger Feb 23 '17 at 10:16
  • sorrry - my fault. missed ^ at the end of a multiline batch file statement. – Jan Viehweger Feb 23 '17 at 10:20
  • 4
    Well, `-P` option isn't working to me (on 18.04), at least not with `-O` option. Is there some other detail I need to pay attention? – artu-hnrq Dec 17 '19 at 17:02
  • 1
    askubuntu's answer sets a new default (with alias in `.bashrc`) without needing to type the command each time: [how to change wget default directory](https://askubuntu.com/a/213529/1024340) – nilon Apr 15 '20 at 20:05
  • @JanViehweger See `wget -x`, which forces it to download to the corresponding local directory that matches each URL pattern if you're using `-i` or `--input-files`. – Andrew Jan 17 '21 at 02:57
  • 2
    @artu-hnrq `-P` is not working with `-O` because `-O` saves to a FILE (see `wget --help` where it outlines this. You cannot save a `wget` URI as a directory **and** a file at the same time. – jon.bray.eth Mar 13 '21 at 04:26
650

-O is the option to specify the path of the file you want to download to:

wget <uri> -O /path/to/file.ext

-P is prefix where it will download the file in the directory:

wget <uri> -P /path/to/folder
Asclepius
  • 57,944
  • 17
  • 167
  • 143
RPradeep
  • 6,564
  • 1
  • 13
  • 11
  • 16
    I'd add a trailing slash to make it `/path/to/folder/` there – Louis Maddox Mar 01 '16 at 16:49
  • 58
    Up voted for also specifying `-O` which I did not need, but made me feel more confident that `-P` was what I needed. – WORMSS Mar 29 '16 at 19:19
  • @louisMaddox per the man pages on my machine, the default prefix is "." (i.e., the current directory) adding a trailing slash to "directory prefix" would cause a double slash error (i.e., my/favorite/dir/prefix//) furthermore, considering the semantics, "directory prefix" would not require a trailing slash. – Timothy L.J. Stewart Apr 07 '17 at 21:14
  • 2
    @TimothyL.J.Stewart There is no double slash error. – alephreish Aug 16 '17 at 14:18
  • 4
    NB: `-O` overrides `-P`, so you can't specify just output directory (think `dirname` **and** just output filename (think `basename`). For this use just `-O` specifying full file path. – murla Apr 05 '20 at 21:47
10

Make sure you have the URL correct for whatever you are downloading. First of all, URLs with characters like ? and such cannot be parsed and resolved. This will confuse the cmd line and accept any characters that aren't resolved into the source URL name as the file name you are downloading into.

For example:

wget "sourceforge.net/projects/ebosse/files/latest/download?source=typ_redirect"

will download into a file named, ?source=typ_redirect.

As you can see, knowing a thing or two about URLs helps to understand wget.

I am booting from a hirens disk and only had Linux 2.6.1 as a resource (import os is unavailable). The correct syntax that solved my problem downloading an ISO onto the physical hard drive was:

wget "(source url)" -O (directory where HD was mounted)/isofile.iso" 

One could figure the correct URL by finding at what point wget downloads into a file named index.html (the default file), and has the correct size/other attributes of the file you need shown by the following command:

wget "(source url)"

Once that URL and source file is correct and it is downloading into index.html, you can stop the download (ctrl + z) and change the output file by using:

-O "<specified download directory>/filename.extension"

after the source url.

In my case this results in downloading an ISO and storing it as a binary file under isofile.iso, which hopefully mounts.

Eje
  • 354
  • 4
  • 8
Compromper
  • 109
  • 1
  • 2
7

"-P" is the right option, please read on for more related information:

wget -nd -np -P /dest/dir --recursive http://url/dir1/dir2

Relevant snippets from man pages for convenience:

   -P prefix
   --directory-prefix=prefix
       Set directory prefix to prefix.  The directory prefix is the directory where all other files and subdirectories will be saved to, i.e. the top of the retrieval tree.  The default is . (the current directory).

   -nd
   --no-directories
       Do not create a hierarchy of directories when retrieving recursively.  With this option turned on, all files will get saved to the current directory, without clobbering (if a name shows up more than once, the
       filenames will get extensions .n).


   -np
   --no-parent
       Do not ever ascend to the parent directory when retrieving recursively.  This is a useful option, since it guarantees that only the files below a certain hierarchy will be downloaded.
pr-pal
  • 3,248
  • 26
  • 18
-1

man wget: -O file --output-document=file

wget "url" -O /tmp/cron_test/<file>
megamind
  • 121
  • 1
  • 8
  • 2
    It's not an answer, becouse -o option put dounloaded file in specified file. But question is "How put file in a folder". And this is duplicate exists answer – Anton Gorbunov Jan 11 '18 at 20:26