Skip download if files already exist in wget?

ParametersDownloadCommand Line-InterfaceWget

Parameters Problem Overview


This is simplest example running wget:

wget http://www.example.com/images/misc/pic.png

but how to make wget skip download if pic.pngis already available?

Parameters Solutions


Solution 1 - Parameters

Try the following parameter:

> -nc, --no-clobber: skip downloads that would download to existing files.

Sample usage:

wget -nc http://example.com/pic.png

Solution 2 - Parameters

The -nc, --no-clobber option isn't the best solution as newer files will not be downloaded. One should use -N instead which will download and overwrite the file only if the server has a newer version, so the correct answer is:

wget -N http://www.example.com/images/misc/pic.png

> Then running Wget with -N, with or without -r or -p, the decision as to whether or not to download a newer copy of a file depends on the local and remote timestamp and size of the file. -nc may not be specified at the same time as -N.

> -N, --timestamping: Turn on time-stamping.

Solution 3 - Parameters

The answer I was looking for is at https://unix.stackexchange.com/a/9557/114862.

> Using the -c flag when the local file is of greater or equal size to the server version will avoid re-downloading.

Solution 4 - Parameters

When running Wget with -r or -p, but without -N, -nd, or -nc, re-downloading a file will result in the new copy simply overwriting the old.

So adding -nc will prevent this behavior, instead causing the original version to be preserved and any newer copies on the server to be ignored.

See more info at GNU.

Solution 5 - Parameters

I had issues with -N as I wanted to save output to a different file name.

Timestamping, wget docs: > A file is considered new if one of these two conditions are met: > 1. A file of that name does not already exist locally. > 2. A file of that name does exist, but the remote file was modified more recently than the local file.

Using test:

test -f stackoverflow.html || wget -O stackoverflow.html https://stackoverflow.com/

If the file exists does not exist test will evaluate to FALSE so wget will be executed.

Solution 6 - Parameters

-nc, --no-clobber

If a file is downloaded more than once in the same directory, wget's behavior depends on a few options, including -nc. In certain cases, the local file is "clobbered" (overwritten), upon repeated download. In other cases, it is preserved.

When running wget without -N, -nc, or -r, downloading the same file in the same directory results in the original copy of file being preserved and the second copy being named file.1. If that file is downloaded yet again, the third copy is named file.2, etc. When -nc is specified, this behavior is suppressed, and wget refuses to download newer copies of file. Therefore, "no-clobber" is a misnomer in this mode: it's not clobbering that's prevented (as the numeric suffixes were already preventing clobbering), but rather the multiple version saving that's being turned off.

When running wget with -r, but without -N or -nc, re-downloading a file results in the new copy overwriting the old. Adding -nc prevents this behavior, instead causing the original version to be preserved and any newer copies on the server to be ignored.

When running wget with -N, with or without -r, the decision as to whether or not to download a newer copy of a file depends on the local and remote timestamp and size of the file. -nc may not be specified at the same time as -N.

Note that when -nc is specified, files with the suffixes .html or .htm are loaded from the local disk and parsed as if they had been retrieved from the web.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnais inpoh ganView Question on Stackoverflow
Solution 1 - ParametersplundraView Answer on Stackoverflow
Solution 2 - ParametersDaniel SokolowskiView Answer on Stackoverflow
Solution 3 - ParametersjstaView Answer on Stackoverflow
Solution 4 - ParametersMaheshView Answer on Stackoverflow
Solution 5 - ParametersrdmolonyView Answer on Stackoverflow
Solution 6 - ParametersEngr AliView Answer on Stackoverflow