如何从Shell脚本获取远程文件大小? [英] How to get remote file size from a shell script?

查看:410
本文介绍了如何从Shell脚本获取远程文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获取像这样的远程文件的大小

Is there a way to get the size of a remote file like

http://api.twitter.com/1/statuses/public_timeline.json

在shell脚本中?

推荐答案

您可以下载文件并获取其大小.但是我们可以做得更好.

You can download the file and get its size. But we can do better.

使用 卷曲 仅获取 ="http://zh.wikipedia.org/wiki/List_of_HTTP_header_fields" rel ="noreferrer"> 响应标头 .

Use curl to get only the response header using the -I option.

在响应标头中查找Content-Length:,其后是文件大小(以字节为单位).

In the response header look for Content-Length: which will be followed by the size of the file in bytes.

$ URL="http://api.twitter.com/1/statuses/public_timeline.json"
$ curl -sI $URL | grep -i Content-Length
Content-Length: 134

要获取大小,请使用过滤器从上面的输出中提取数字部分:

To get the size use a filter to extract the numeric part from the output above:

$ curl -sI $URL | grep -i Content-Length | awk '{print $2}'
134

这篇关于如何从Shell脚本获取远程文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆