wget Downloading and replacing file only if target is newer than source - linux

This is what I'm trying to achieve :
User uploads file1.jpg to Server A
Using wget Server B only downloads file1.jpg from Server A if the file is newer than the one that already exists on Server B and then replaces the file on Server B with the new one.
I know I can use :
wget -N http://www.mywebsite.com/files/file1.jpg
To check that the target file is newer than the source but I'm a little confused as to how I format the command to let it know what and where the actual source file is?
Is it something like? :
wget -N http://www.mywebsite.com/files/file1.jpg /serverb/files/file1.jpg
Cheers!

You can use -P option to specify the directory where the file(s) will be downloaded:
$ wget -N -P /serverb/files/ http://www.mywebsite.com/files/file1.jpg
You are also talking about downloading and replacing the file. Be aware, that wget overwrites the file, so it is "broken" while being downloaded. I don't think you can do atomic replacement of the file using only wget. You need a small script that uses temporary files and move to atomically replace the file in Server B.

Related

wget to download new wildcard files and overwrite old ones

I'm currently using wget to download specific files from a remote server. The files are updated every week, but always have the same file names. e.g new upload file1.jpg will replace local file1.jpg
This is how I am grabbing them, nothing fancy :
wget -N -P /path/to/local/folder/ http://xx.xxx.xxx.xxx/remote/files/file1.jpg
This downloads file1.jpg from the remote server if it is newer than the local version then overwrites the local one with the new one.
Trouble is, I'm doing this for over 100 files every week and have set up cron jobs to fire the 100 different download scripts at specific times.
Is there a way I can use a wildcard for the file name and have just one script that fires every 5 minutes for example?
Something like....
wget -N -P /path/to/local/folder/ http://xx.xxx.xxx.xxx/remote/files/*.jpg
Will that work? Will it check the local folder for all current file names, see what is new and then download and overwrite only the new ones? Also, is there any danger of it downloading partially uploaded files on the remote server?
I know that some kind of file sync script between servers would be a better option but they all look pretty complicated to set up.
Many thanks!
You can specify the files to be downloaded one by one in a text file, and then pass that file name using option -i or --input-file.
e.g. contents of list.txt:
http://xx.xxx.xxx.xxx/remote/files/file1.jpg
http://xx.xxx.xxx.xxx/remote/files/file2.jpg
http://xx.xxx.xxx.xxx/remote/files/file3.jpg
....
then
wget .... --input-file list.txt
Alternatively, If all your *.jpg files are linked from a particular HTML page, you can use recursive downloading, i.e. let wget follow links on your page to all linked resources. You might need to limit the "recursion level" and file types in order to prevent downloading too much. See wget --help for more info.
wget .... --recursive --level=1 --accept=jpg --no-parent http://.../your-index-page.html

wget:: How to rename all the already dowloaded files as name given by wget --content-dispostion?

I have downloaded 1200 jpeg files using wget. But the name of the files are based on links from which they are downloaded.
For ex.
http://www.*.*.*/index.php?id=0MwfTcqbP9dl1_icR3_gVezE8tlpUJt-wumA5hHjpjk will download the file with name index.php?id=0MwfTcqbP9dl1_icR3_gVezE8tlpUJt-wumA5hHjpjk.jpg but its name on server is different. Now I want all the files to be named as the name on the server.
One way is to delete all the files and re-download it with wget option --content-dispostion but total size of the download is 8GB and downloading it again is not a good option.
How can I rename all the downloaded files as names on server?
Edit: Name of the jpeg files downloaded from the links using wget --content-disposition or browser would be like 2014:08:09_18:07:51_IMG_5543.jpg (not created by wget, it's oringinal name on server, uploader's file name). I want all the files to be named as their oringinal names without again downloading them.
If the webserver supports HEAD request, you can use commands like wget --server-response --spider $URL, otherwise you can use range 0-1 to get one byte only. After you have the response heahers, you can write a script to rename. – Wu Yongzheng

Using wget on a directory

I'm fairly new to shell and I'm trying to use wget to download a .zip file from one directory to another. The only file in the directory I am copying the file from is the .zip file. However when I use wget IP address/directory it downloads an index.html file instead of the .zip. Is there something I am missing to get it to download the .zip without having to explicitly state it?
wget is the utility to download file from web.
you have mentioned you want to copy from one directory to other. you meant it is on same server/node?
In that case you can simply use cp command
And if you want if from any other server/node [file transfer] you can use scp or ftp

How to append the wget downloaded file?

I am running wget through cronjob for executing some script in scheduled manner. Everytime the output is downloaded and saved as new file. I want to append the output to same file. How can I do that?
I am talking about the downloaded content from the URL but not the log of the execution.
You can do it using the following command:
wget <URL> -O ->> <FILE_NAME>
My first approach now would be to download it to a file, add the content of the new downloaded file to the previously downloaded file and delete it.

checksum remote file

Is there a way to get a program I can run via the command line that would do a checksum of a remote file? For instance get a checksum of https://stackoverflow.com/opensearch.xml
I want to be able get an update of when a new rss/xml entry is available. I was thinking I could do a checksum of a file every once in a while and if it is different then there must be an update. I'm looking to write a shell script that checks new rss/xml data.
A quick way to do this with curl is to pipe the output to sha1sum as follows:
curl -s http://stackoverflow.com/opensearch.xml|sha1sum
In order to make a checksum on the file, you'll have to download it first.
Instead of this, use If-Modified-Since in your request headers, and server will respond with 304 not modified header and without content, if the file is not changed, or with the content of the file, if it was changed. You may be interested also in checking for ETag support on the server.
If downloading the file is not a problem, you can use md5_file to get md5 checksum of the file
curl
curl has an '-z' option:
-z/--time-cond <date expression>|<file>
(HTTP/FTP) Request a file that has been modified later
than the given time and date, or one that has been modified before
that time. The <date expression> can be all sorts of date strings
or if it doesn't match any internal ones, it is taken as a filename
and tries to get the modification date (mtime) from <file> instead.
See the curl_getdate(3) man pages for date expression details.
So what you can do is:
$ curl http://stackoverflow.com/opensearch.xml -z opensearch.xml -o opensearch.xml
This will do actual download if remote file is younger than the local one (local file may absent - in this case it will be downloaded). Which seems to be exactly what you need...
wget
wget also has an option to track timestamps - -N
When 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.
-N, --timestamping Turn on time-stamping.
So in case wget one can use:
$ wget -N http://stackoverflow.com/opensearch.xml
You can try this under your bash:
wget <http://your file link>
md5sum <your file name>
You should first examine the HTTP headers to see if the server itself is willing to tell you when the file is from; it's considered bad form to fetch the entire file if you don't need to.
Otherwise, you'll need to use something like wget or curl to fetch the file, so I really hope you don't plan to be working with anything large.

Resources