如何从 shell 脚本获取远程文件大小?
有没有办法像 shell 脚本一样获取远程文件的大小
http://api.twitter.com/1/statuses/public_timeline.json
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有没有办法像 shell 脚本一样获取远程文件的大小
http://api.twitter.com/1/statuses/public_timeline.json
?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(14)
您可以下载该文件并获取其大小。但我们可以做得更好。
使用 curl 仅获取 响应标头使用
-I
选项。在响应标头中查找
Content-Length:
,其后是文件大小(以字节为单位)。要获取大小,请使用过滤器从上面的输出中提取数字部分:
You can download the file and get its size. But we can do better.
Use curl to get only the response header using the
-I
option.In the response header look for
Content-Length:
which will be followed by the size of the file in bytes.To get the size use a filter to extract the numeric part from the output above:
对其他答案的两个警告:
另外,您可以在没有 grep/awk 或管道的情况下执行此操作:
并且使用压缩来执行相同的请求:
Two caveats to the other answers:
Also, you can do this without grep/awk or piping:
And the same request with compression:
类似于 codaddict 的回答 ,但没有调用
grep
:Similar to codaddict's answer, but without the call to
grep
:我认为最简单的方法是:
使用 cURL 以静默模式运行
-s
,仅拉动标头
-I
(以避免下载整个文件)然后执行不区分大小写的 grep
-i
并返回第二个arg 使用 awk
$2
.输出以
字节
形式返回示例:
或
或
显示为千字节/兆字节
如果您想以千字节为单位显示大小然后将 awk 更改为:
或兆字节
I think the easiest way to do this would be to:
use cURL to run in silent mode
-s
,pull only the headers
-I
(so as to avoid downloading the whole file)then do a case insensitive grep
-i
and return the second arg using awk
$2
.output is returned as
bytes
Examples:
or
or
Show as Kilobytes/Megabytes
If you would like to show the size in Kilobytes then change the awk to:
or Megabytes
当存在重定向时,前面的答案将不起作用。例如,如果想要 debian iso DVD 的大小,则必须使用 --location 选项,否则,报告的大小可能是
302 Moved Temporarily
答案正文的大小,而不是真实文件。假设您有以下 url:
使用curl,您可以获得:
这就是为什么我更喜欢使用
HEAD
,它是 中lwp-request
命令的别名libwww-perl 软件包(在 debian 上)。它的另一个优点是它去除了额外的 \r 字符,从而简化了后续的字符串处理。因此,要检索 debian iso DVD 的大小,可以这样做:
请注意:
对于其他 shell,您可能有求助于 sed、awk、grep 等。
The preceding answers won't work when there are redirections. For example, if one wants the size of the debian iso DVD, he must use the --location option, otherwise, the reported size may be that of the
302 Moved Temporarily
answer body, not that of the real file.Suppose you have the following url:
With curl, you could obtain:
That's why I prefer using
HEAD
, which is an alias to thelwp-request
command from the libwww-perl package (on debian). Another advantages it has is that it strips the extra \r characters, which eases subsequent string processing.So to retrieve the size of the debian iso DVD, one could do for example:
Please note that:
For other shells, you may have to resort to sed, awk, grep et al..
接受的解决方案对我不起作用,这是:
The accepted solution was not working for me, this is:
我有一个基于codaddict的答案的shell函数,它以人类可读的格式给出远程文件的大小:
I have a shell function, based on codaddict's answer, which gives a remote file's size in a human-readable format thusly:
这将向您显示有关正在进行的下载的详细信息,
您只需指定一个 URL,如下例所示。
输出
This will show you a detailed info about the ongoing download
you just need to specify an URL like below example.
output
将以上所有内容结合起来对我来说是可行的:
这将仅返回内容长度(以字节为单位):
To combine all the above for me works:
This will return just the content length in bytes:
您可以这样做,包括自动遵循
301/302
重定向:这是非常暴力的方法,但可以完成工作 - 但这是服务器报告的原始值,因此您可能必须根据需要对其进行调整。
您可能还需要添加
-g
标志,以便它可以自动处理从普通http
到https
的切换:You can kinda do it like this, including auto-following
301/302
redirections :It's very brute force but gets the job done - but that's whatever raw value being reported by the server, so you may have to make adjustments to it as you see fit.
You may also have to add the
-g
flag so it can auto handle switchover from vanillahttp
tohttps
:问题很旧并且已经得到了充分的回答,但让我们扩展现有的答案。如果您想自动执行此任务(用于检查多个文件的文件大小),那么这里有一个行。
首先将文件的 URL 写入文件中:
cat url_of_files.txt
然后从命令行(与
url_of_files.txt
位于同一目录):这是用于检查文件大小范围从
字节
到Gbs
。我使用这条线来检查 JWST 团队提供的拟合数据文件。它检查文件大小,并根据其大小,将其粗略地转换为适当的数字,扩展名为 B、K、M、G,表示大小(以字节、千字节、兆字节和千兆字节为单位)。
结果:
Question is old and have been sufficiently answered , but let expand upon exisiting answer. If you want to automate this task ( for checking file sizes of multiple files) then here's a one liner.
first write the URL of the files in a file:
cat url_of_files.txt
then from the command line (from the same directory as your
url_of_files.txt
):This is for checking file sizes ranging from
bytes
toGbs
. I use this line to check the fits data files being made available by the JWST team.It checks the file size and depending on its size , roughly converts it to a an appropriate number with B,K,M,G extensions denoting the size in Bytes, Kilo bytes, Mega bytes, and Giga bytes.
result:
我的解决方案是使用 awk
END
来确保仅 grep 最后一个Content-length
:10806508
事实上,如果没有它,就会是
My solution is using awk
END
to ensure to grep only the lastContent-length
:10806508
In fact without it would have been
我像这样使用
([Cc]ontent-[Ll]ength:)
,因为我让服务器在标头响应中给出多个 Content-Length 字符Accept-Ranges: bytes
访问控制公开标头:日期、服务器、内容类型、内容长度
服务器:WowzaStreamingEngine/4.5.0
缓存控制:无缓存
访问控制允许来源:*
访问控制允许凭据: true
访问控制允许方法:OPTIONS、GET、POST、HEAD
访问控制允许标头:内容类型、用户代理、If-Modified-Since、缓存控制、范围
日期:2017 年 1 月 10 日星期二 01:56:08 GMT
内容类型:视频/MP2T
内容长度:666460
I use like this
([Cc]ontent-[Ll]ength:)
, because I got server give multiple Content-Length character at header responseAccept-Ranges: bytes
Access-Control-Expose-Headers: Date, Server, Content-Type, Content-Length
Server: WowzaStreamingEngine/4.5.0
Cache-Control: no-cache
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: OPTIONS, GET, POST, HEAD
Access-Control-Allow-Headers: Content-Type, User-Agent, If-Modified-Since, Cache-Control, Range
Date: Tue, 10 Jan 2017 01:56:08 GMT
Content-Type: video/MP2T
Content-Length: 666460
不同的解决方案:
为您提供以 KB 为单位的大小
different solution:
gives you the size in KB