823

I am downloading a file using the wget command. But when it downloads to my local machine, I want it to be saved as a different filename.

For example: I am downloading a file from www.examplesite.com/textfile.txt

I want to use wget to save the file textfile.txt on my local directory as newfile.txt. I am using the wget command as follows:

wget www.examplesite.com/textfile.txt
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
noobcoder
  • 11,983
  • 10
  • 39
  • 62
  • 71
    It's `wget -O newfile.txt`. – Mechanical snail May 21 '13 at 20:04
  • but wget -o will simply give you the progress of downloading as a logfile. I tried wget -o. – noobcoder May 21 '13 at 20:04
  • 39
    `wget -o` will output log information to a file. `wget -O` will output the downloaded content. `man wget` will tell you all of this and more. – Andy Ross May 21 '13 at 20:06
  • 4
    What @AndyRoss said: mind the capitalisation; it's commonly MEANINGFUL in unix/linux land. – tink May 21 '13 at 20:07
  • Ohhh, I missed out on the Caps. I was trying the lower case o until @AndyRoss pointed out. It worked. – noobcoder May 21 '13 at 20:10
  • 1
    This does not answer the question at all. The OUTPUT of the command will be saved to -o file, but the file itself will be downloaded with the same name and not "saved as". – John Mikic Aug 22 '15 at 18:26
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Sep 09 '18 at 16:28
  • Is there a way to get the filename that `wget` will use for the output file? I'd like to use `wget`'s default name and then process the file afterwards, but to do the post processing I need the name that `wget` used for the file. – BallpointBen Mar 11 '21 at 18:52

6 Answers6

1188

Use the -O file option.

E.g.

wget google.com
...
16:07:52 (538.47 MB/s) - `index.html' saved [10728]

vs.

wget -O foo.html google.com
...
16:08:00 (1.57 MB/s) - `foo.html' saved [10728]
thutt
  • 640
  • 5
  • 18
naumcho
  • 18,671
  • 14
  • 48
  • 59
  • 42
    Man pages reveal: "Use of -O is not intended to mean simply 'use the name file instead of the one in the URL;' rather, it is analogous to shell redirection: ``wget -O file http://foo`` is intended to work like ``wget -O - http://foo > file``; file will be truncated immediately, and all downloaded content will be written there." –  Jul 28 '15 at 15:35
  • 1
    How would one append to foo.html instead of overwriting it? – Craig Jacobs Jun 25 '16 at 21:51
  • 4
    @CraigJacobs you can output `wget` in console and append it to file. For example `wget -O - -o /dev/null http://google.com >> foo.html`. [Reference](http://serverfault.com/questions/25779/how-do-i-pipe-a-downloaded-file-to-standard-output-in-bash) – Deepak Chaudhary Aug 06 '16 at 10:47
  • Thanks @javadba, on Mint 18 (based on Ubuntu 16.04) only your syntax works, as it seems true for Centos6 as seen in other answer (the answer is edited pending acceptance) – ElMesa Nov 24 '16 at 12:26
  • 3
    This approach would render --timestamping ineffective – Hamy Jun 20 '17 at 05:11
  • 1
    This command won't create the file if the directory doesn't exist before – Freedo Jun 02 '19 at 14:57
  • 1
    Not to be confused with `-o`! – HackerDaGreat57 Oct 28 '22 at 05:11
  • Is it possible to merely choose the file stem, and let `wget` apply the extension of the file you're downloading? Ex `wget -P /my/dir --stem foo https://example.com/img.jpg` → `/my/dir/foo.jpg` – BallpointBen Jul 24 '23 at 21:58
154

Also notice the order of parameters on the command line. At least on some systems (e.g. CentOS 6):

wget -O FILE URL

works. But:

wget URL -O FILE

does not work.

mousomer
  • 2,632
  • 2
  • 24
  • 25
45

You would use the command Mechanical snail listed. Notice the uppercase O. Full command line to use could be:

wget www.examplesite.com/textfile.txt --output-document=newfile.txt

or

wget www.examplesite.com/textfile.txt -O newfile.txt

Hope that helps.

mpm
  • 3,534
  • 23
  • 33
ioMatrix
  • 603
  • 5
  • 6
  • 1
    `--output-document=newfile.txt` is what worked for me. All attempts to use -O failed with the error `Resolving webmin_1.630_all.deb (webmin_1.630_all.deb)... failed: Name or service not known.` – KalenGi Feb 18 '21 at 14:14
28

Either curl or wget can be used in this case. All 3 of these commands do the same thing, downloading the file at http://path/to/file.txt and saving it locally into "my_file.txt".

Note that in all commands below, I also recommend using the -L or --location option with curl in order to follow HTML 302 redirects to the new location of the file, if it has moved. wget requires no additional options to do this, as it does this automatically.

# save the file locally as my_file.txt

wget http://path/to/file.txt -O my_file.txt  # my favorite--it has a progress bar
curl -L http://path/to/file.txt -o my_file.txt
curl -L http://path/to/file.txt > my_file.txt

Alternatively, to save the file as the same name locally as it is remotely, use either wget by itself, or curl with -O or --remote-name:

# save the file locally as file.txt

wget http://path/to/file.txt
curl -LO http://path/to/file.txt
curl -L --remote-name http://path/to/file.txt

Notice that the -O in all of the commands above is the capital letter "O".

The nice thing about the wget command is it shows a nice progress bar.

You can prove the files downloaded by each of the sets of 3 techniques above are exactly identical by comparing their sha512 hashes. Running sha512sum my_file.txt after running each of the commands above, and comparing the results, reveals all 3 files to have the exact same sha hashes (sha sums), meaning the files are exactly identical, byte-for-byte.

References

  1. I learned about the -L option with curl here: Is there a way to follow redirects with command line cURL?

See also: How to capture cURL output to a file?

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
24
wget -O yourfilename.zip remote-storage.url/theirfilename.zip

will do the trick for you.

Note:

a) its a capital O.

b) wget -O filename url will only work. Putting -O last will not.

Lakshman Pilaka
  • 1,803
  • 2
  • 24
  • 48
11

Using CentOS Linux I found that the easiest syntax would be:

wget "link" -O file.ext

where "link" is the web address you want to save and "file.ext" is the filename and extension of your choice.

dhh
  • 4,289
  • 8
  • 42
  • 59
ZenkyGt
  • 119
  • 1
  • 2
  • 8
    I dont understand answering an already-answered-question with the same answer 2 years later.. – alegria Nov 19 '20 at 18:41