Is there a way to get the size of a remote file like
http://api.twitter.com/1/statuses/public_timeline.json
in shell script?
Is there a way to get the size of a remote file like
http://api.twitter.com/1/statuses/public_timeline.json
in shell script?
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:
This will show you a detailed info about the ongoing download
you just need to specify an URL like below example.
output
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..
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
I have a shell function, based on codaddict's answer, which gives a remote file's size in a human-readable format thusly: