Linux 校验和远程文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6801704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:13:56  来源:igfitidea点击:

checksum remote file

linuxcommand-linecurlterminalchecksum

提问by rich

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

有没有办法让我可以通过命令行运行的程序可以对远程文件进行校验?例如获取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.

我希望能够获得新的 rss/xml 条目何时可用的更新。我在想我可以每隔一段时间对文件进行一次校验和,如果它不同,那么必须进行更新。我正在寻找编写一个检查新 rss/xml 数据的 shell 脚本。

回答by Maxim Krizhanovsky

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-Modified-Since,如果文件未更改,则服务器将响应 304 未修改标头且没有内容,或者文件内容(如果已更改)。您可能还对检查服务器上的 ETag 支持感兴趣。

If downloading the file is not a problem, you can use md5_fileto get md5 checksum of the file

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

回答by Olipro

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.

您应该首先检查 HTTP 标头,看看服务器本身是否愿意告诉您该文件的来源;如果您不需要,则获取整个文件被认为是不好的形式。

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.

否则,您将需要使用 wget 或 curl 之类的东西来获取文件,所以我真的希望您不打算使用任何大的东西。

回答by Kit Ho

You can try this under your bash:

你可以在你的 bash 下试试这个:

wget <http://your file link>

md5sum <your file name>

回答by Andrew Krasny

curl

卷曲

curl has an '-z' option:

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.

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 timespamts - -N

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.

So in case wget one can use:

因此,万一 wget 可以使用:

$ wget -N http://stackoverflow.com/opensearch.xml

回答by elec3647

A quick way to do this with curl is to pipe the output to sha1sum as follows:

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

curl -s http://stackoverflow.com/opensearch.xml|sha1sum