校验和远程文件

ric*_*ich 8 linux terminal command-line curl checksum

有没有办法获得一个程序,我可以通过命令行运行,该程序将执行远程文件的校验和?例如,获取https://stackoverflow.com/opensearch.xml的校验和

我希望能够获得有关新rss/xml条目何时可用的更新.我以为我可以偶尔对文件进行校验和,如果它不同则必须有更新.我正在寻找一个shell脚本来检查新的rss/xml数据.

Max*_*sky 5

为了对文件进行校验和,您必须先下载它.取而代之的是,在请求标头中使用If-Modified-Since,服务器将使用304未修改的标头和没有内容(如果文件未更改)或文件内容(如果已更改)进行响应.您可能也有兴趣检查服务器上的ETag支持.

如果下载文件不是问题,可以使用md5_file获取文件的md5校验和


小智 5

卷曲

curl 有一个“-z”选项:

   -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.
Run Code Online (Sandbox Code Playgroud)

所以你可以做的是:

$ curl http://stackoverflow.com/opensearch.xml -z opensearch.xml -o opensearch.xml
Run Code Online (Sandbox Code Playgroud)

如果远程文件比本地文件年轻,这将进行实际下载(本地文件可能不存在 - 在这种情况下将被下载)。这似乎正是你所需要的......

获取

wget 也有跟踪时间戳的选项 - -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.
Run Code Online (Sandbox Code Playgroud)

因此,万一 wget 可以使用:

$ wget -N http://stackoverflow.com/opensearch.xml
Run Code Online (Sandbox Code Playgroud)


小智 5

使用curl执行此操作的快速方法是将输出通过管道输出到sha1sum,如下所示:

curl -s http://stackoverflow.com/opensearch.xml|sha1sum
Run Code Online (Sandbox Code Playgroud)

  • 我也添加了`-L`...`curl -sL URL | sha256sum` (macOS) 或 `curl -sL URL | 沙苏姆 -a 256` ... (4认同)