乐闻世界logo
搜索文章和话题

How to force wget to overwrite an existing file ignoring timestamp?

1个答案

1

When using the wget command to download files, by default, if a file with the same name already exists locally, wget checks the file timestamp to determine whether to overwrite the local file. If the file on the server is newer than the local file, it will overwrite; otherwise, it typically skips downloading the file.

To make wget ignore the timestamp and force overwrite the local file, use the -O option (or --output-document) instead of -N (or --timestamping), which specifies the output filename and overwrites it regardless of its existence.

For example, to download a file named example.txt, use the command:

bash
wget -O example.txt http://www.example.com/example.txt

The -O example.txt option tells wget to store the downloaded content as example.txt, overwriting it regardless of whether it already exists.

If you want to download multiple files and force overwrite the local files for each, you may need to write a script to loop through each download URL and use the above command.

For instance, if you regularly update data from a weather data source in an automation script, you can write:

bash
#!/bin/bash wget -O daily-report.txt https://weatherdata.example.com/daily-report.txt wget -O weekly-report.txt https://weatherdata.example.com/weekly-report.txt

This script ensures that every time it runs, regardless of the local file's timestamp, it downloads the latest daily and weekly reports from the specified URLs and overwrites the local daily-report.txt and weekly-report.txt files. This is very useful when handling data that requires frequent updates.

2024年7月30日 00:22 回复

你的答案