159

I need to use wget to download a file to the directory /var/cache/foobar/ (so, as an example, if I download stackexchange-site-list.txt, it'd be downloaded to /var/cache/foobar/stackexchange-site-list.txt)

Is this possible? curl would also be an option, but I'd prefer to not use curl, since it's not installed by default.

5
  • 7
    FYI, This information is available in man wget
    – user606723
    Oct 28, 2011 at 16:28
  • 8
    @user606723 Can you explain why this got downvotes? It's a legitimate question.
    – jrg
    Oct 31, 2011 at 12:29
  • 4
    it's my guess that it was down voted because this is an extremely basic question and would've been solved by any sort of googling or looking in man get. The goal of stackexchange isn't to answer every possible question.
    – user606723
    Dec 11, 2011 at 22:38
  • 1
    This SO Q&A explains that you can do this using the -P switch, same w/ the A below. From the manual page: How to specify the location with wget?.
    – slm
    May 7, 2015 at 23:33
  • 9
    This is only a basic question to those who already know the answer. To anyone who doesn’t, it is not basic. Searching the man pages is not as simple as it sounds because they are quite long and few sane people with lives have the time or wherewithal to sit and read through all of that. The best argument you can make is that the man pages could be filtered with grep or something, but even that is not likely to give the answer due to wording.
    – Synetech
    Apr 10, 2016 at 23:10

3 Answers 3

237
wget -P /var/cache/foobar/ [...]
wget --directory-prefix=/var/cache/foobar/ [...]
3
  • 3
    -P lets wget start creating sub folders depending on how you select options to download.
    – Kendrick
    Apr 28, 2014 at 15:07
  • 13
    -P seems to be the synonym for --directory-prefix. From the manual: ‘-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). Feb 20, 2015 at 17:29
  • It seems to be silently & recursively creating the specified directoriy(s), if one either mistyped an actual directory path, or just outright non existent one. Additionally, the long form of the flag --directory-prefix= seems to be either not working correctly with shell expansion, or always ignites every path from the current relative path
    – polendina
    Sep 7, 2022 at 4:08
88

If you know the name of the file ahead of time, you can use the -O option to wget to tell it where to write the file.

wget -O /var/cache/foobar/stackexchange-site-list.txt http://url.to/stackexchange-site-list.txt
4
  • 22
    @jrg: You can specify a download directory. See ephemient's answer.
    – unor
    Nov 27, 2012 at 16:32
  • 1
    right is that -0 or -O, I guess can just copy it and see Sep 6, 2019 at 18:37
  • its a minus capital O if that helps
    – mithunpaul
    Oct 3, 2019 at 0:31
  • 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
    – nilon
    Apr 15, 2020 at 20:05
1
wget -e robots=off -r --no-parent /home/user/Desktop/MIT/ http://abc.tamu.edu/projects/tzivi/repository/revisions/2/raw/tzivi/

To add: The above code would allow you to download ALL files from the targeted directory to the directory of your choice in a single command.

Break Down of Command:

The Parameters for which wget uses to download the entire directory at once instead of one file at a time:

wget -e robots=off -r --no-parent

The Destination of Downloaded Files

/home/user/Desktop/MIT/

The Origin of Files (Online Directory)

http://abc.tamu.edu/projects/tzivi/repository/revisions/2/raw/tzivi/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .