Using wget command, how do I allow/instruct to overwrite my local file everytime, irrespective of how many times I invoke.

Let's say, I want to download a file from the location: http://server/folder/file1.html

Here, whenever I say wget http://server/folder/file1.html, I want this file1.html to be overwritten in my local system irrespective of the time it is changed, already downloaded, etc. My intention/use case here is that when I call wget, I'm very sure that I want to replace/overwrite the existing file.

I've tried out the following options, but each option is intended/meant for some other purpose.

  1. -nc => --no-clobber
  2. -N => Turn on time-stamping
  3. -r => Turn on recursive retrieving
link|improve this question

68% accept rate
feedback

8 Answers

up vote 2 down vote accepted
wget -q http://www.whatever.com/filename.txt -O /path/filename.txt 

-q is quiet mode so you can throw it in a cron without any output from the command

link|improve this answer
There is no direct option in wget command that does this without me specifying explicitly using -O filename? – Gnanam Aug 17 '10 at 13:14
3  
It seems that there is no way to force overwriting every files when downloading files using wget. However, use -N option can surely force downloading and overwriting newer files. wget -N Will overwrite original file if the size or timestamp change – aleroot Aug 17 '10 at 13:21
Not true. direct the output of the command into stdout and pipe it to the file: wget -q $urlYouNeedToGrap > $fileYouWantToOverwriteEverytime – rasjani Aug 21 '10 at 22:35
feedback

Use curl instead?

curl http://server/folder/file1.html > file1.html
link|improve this answer
I'm not a Linux expert. What is the basic difference between wget and curl? I'm sure that each command is meant for some specific purpose. – Gnanam Aug 17 '10 at 13:27
1  
@Gnanam: They overlap a lot in basic CLI utility, actually. Both can make an HTTP connection and save the result to disk. For a run down on the differences check out daniel.haxx.se/docs/curl-vs-wget.html Regardless, the above usage is complete valid. There are other tools in this general area, too: curl.haxx.se/docs/comparison-table.html – Stu Thompson Aug 17 '10 at 14:13
Those 2 links are really helpful to understand the difference. – Gnanam Aug 18 '10 at 4:48
feedback

I don't think you can do it unless you also download the directories (so pass the -x flag). If you know what the file is, you can do use -O filename, so for example:
wget http://yourdomain.com/index.html -O index.html

link|improve this answer
feedback

Untried: maybe you can work with wget -r --level=0.

Another possibility: curl -O overwrites (but it uses a different way of choosing the file name, which may or may not matter to you).

link|improve this answer
feedback

Why not put a small wrapper around the wget in your script?

The script could move all the files to a temporary location, then wget the remote files / web pages.

On success delete the files in the temporary location. On failure move the files back and raise an error.

There isn't a simple way to do what you want using just wget unless you know specifically the name of all files, in which case the -O option will allow you to force the filename of the file downloaded.

link|improve this answer
feedback

Like this:

wget -q $URL > $FILE

link|improve this answer
wget -qO- $URL > $FILE fischerlaender.net/webdev/redirecting-wget-to-stdout – Steven Penny Mar 22 at 8:48
feedback

OK - all the answers look confusing (Like 10 random color of wires, which one would i cut now!! blue one or red one!!, if i now do the blue one it may explode. lol )

i need a correct answer which works and bullet proof. Let me put some input/output :)

[curl]

Test 1: -------------------------------------------------------- [FAIL]

$ touch /var/www/html/video/filmfr2.avi;
$ touch /tmp/filmfr2.avi;

$ date; cd /tmp; curl -O http://localhost:7007/video/filmfr2.avi; ll filmfr2.avi;
Mon Jan 23 16:36:17 CET 2012
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
-rw-rw-r-- 1 sun sun 0 Jan 23 16:32 filmfr2.avi


Test 2: ---------------------------------------------------------- [OK]

$ touch /var/www/html/video/filmfr2.avi;
$ touch /tmp/filmfr2.avi;
$ date; cd /tmp; curl -O http://localhost:7007/video/filmfr2.avi > /tmp/filmfr2.avi ; ll filmfr2.avi;
Mon Jan 23 16:38:24 CET 2012
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
-rw-rw-r-- 1 sun sun 0 Jan 23 16:38 filmfr2.avi

[wget]

Test 3: ----------------------------------------------------------- [FAIL]


$ touch /var/www/html/video/filmfr2.avi;
$ touch /tmp/filmfr2.avi;
$ date; cd /tmp; wget -q http://localhost:7007/video/filmfr2.avi -O /tmp/filmfr2.avi ; ll filmfr2.avi
Mon Jan 23 16:41:33 CET 2012
-rw-rw-r-- 1 sun sun 0 Jan 23 16:33 filmfr2.avi

Test 4: ------------------------------------------------------------ [OK]

$ touch /var/www/html/video/filmfr2.avi;
$ touch /tmp/filmfr2.avi;
$ date; cd /tmp; wget -q http://localhost:7007/video/filmfr2.avi > /tmp/filmfr2.avi ; ll filmfr2.avi
Mon Jan 23 16:43:07 CET 2012
-rw-rw-r-- 1 sun sun 0 Jan 23 16:43 filmfr2.avi
link|improve this answer
feedback

This option works

wget -N http://server/folder/file1.html
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.