Linux 通过wget在服务器上下载最新文件

Linux 通过wget在服务器上下载最新文件,linux,bash,unix,ubuntu,wget,Linux,Bash,Unix,Ubuntu,Wget,大家下午好 我试图弄清楚如何在Linux系统上使用wget从服务器下载最新的文件。这些文件是5分钟的雷达数据,因此,到最近一次,即1930.grib2、1935.grib2、1940.grib2等,文件增加了5分钟 目前,我在bash脚本中实现了以下代码,从每个小时的顶部开始下载每个文件,但这不是获取最新文件的有效方法: HR=$(date +%H) padtowidth=2 START=0 END=55 i=${START} while [[ ${i} -le ${END} ]] do t

大家下午好

我试图弄清楚如何在Linux系统上使用wget从服务器下载最新的文件。这些文件是5分钟的雷达数据,因此,到最近一次,即1930.grib2、1935.grib2、1940.grib2等,文件增加了5分钟

目前,我在bash脚本中实现了以下代码,从每个小时的顶部开始下载每个文件,但这不是获取最新文件的有效方法:

HR=$(date +%H)
padtowidth=2
START=0
END=55
i=${START}

while [[ ${i} -le ${END} ]]
do

tau=$(printf "%0*d\n" $padtowidth ${i})

URL1=http://thredds.ucar.edu/thredds/fileServer/grib/nexrad/composite/unidata/files/${YMD}/Level_3_Composite_N0R_${YMD}_${HR}${tau}.grib2

wget -P ${HOMEDIR}${PATH1}${YMD}/${HR}Z/ -N ${URL1}

((i = i + 5))
done

如果有一个所有文件的索引,您可以先下载它,然后解析它以查找最新的文件

如果不可能,您可以从当前时间向后计数(除了使用
date+%H
之外,还可以使用
date+%M
),并在
wget
能够获取文件时停止(例如,如果
wget
0
退出)

希望有帮助


解析索引的示例:

filename=`wget -q -O - http://thredds.ucar.edu/thredds/catalog/grib/nexrad/composite/unidata/NEXRAD_Unidata_Reflectivity-20140501/files/catalog.html | grep '<a href=' | head -1 | sed -e 's/.*\(Level3_Composite_N0R_[0-9]*_[0-9]*.grib2\).*/\1/'`

filename=`wget-q-O-http://thredds.ucar.edu/thredds/catalog/grib/nexrad/composite/unidata/NEXRAD_Unidata_Reflectivity-20140501/files/catalog.html GRP'

我自动制作了一个C++控制台程序。我将在下面发布整个代码。只需使用wget捕获目录文件,然后在同一目录中运行它,它将自动创建一个BAT文件,您可以随意启动该文件以下载最新的文件。我是专门为Unidata THREDDS服务器写的,所以我知道这是一个很好的答案。编辑和重要提示:这是最新的GOES-16数据,因此您必须处理不同产品的子字符串值

#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;


int main() 

{

// First, I open the catalog.html which was downloaded using wget, and put the entire file into a string.

ifstream inFile; // create instance
inFile.open("catalog.html"); // opens the file
stringstream strStream; // create stringstream
strStream << inFile.rdbuf();  //read the file
string str = strStream.str();  //str holds the content of the file

cout << str << endl;  // The string contains the entire catalog ... you can do anything with the string

// Now I will create the entire URL we need automatically by getting the base URL which is known (step 1 is : string "first")

string first= "http://thredds-test.unidata.ucar.edu/thredds/fileServer/satellite/goes16/GRB16/ABI/CONUS/Channel02/current/";

// The string "second" is the actual filename, since (to my knowledge) the filename in the HTML file never changes, but this must be watched in case it DOES change     in the future. I use the c++ substring function to extract it.

string second = str.substr(252784,76); 


// I then create a batch file and write "wget (base url + filename)" which can now automatically launch/download the latest GRIB2 file.

ofstream myfile2;
myfile2.open ("downloadGOESLatest.bat");
myfile2 << "wget ";
myfile2 << first;
myfile2 << second;
myfile2.close();


return 0;

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
//首先,我打开使用wget下载的catalog.html,并将整个文件放入字符串中。
ifstream infle;//创建实例
inFile.open(“catalog.html”);//打开文件
stringstream stream;//创建stringstream

strStream你能说得更详细吗?这里有大量的例子,这就是为什么我说得更简洁。这里有手册页,你也可以用谷歌搜索rsync的例子。你好,莫福,谢谢!你能有机会发布一个解析例子的链接吗?我试过了,但运气不好。这很大程度上取决于索引的格式。你能发布这个吗页面的来源或URL?你可以试试这个:filename=`wget-q-O-|grep'哇,谢谢!那么,我会在终端上运行这个,或者把它放到我的bash脚本中?