Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get remote file size from a shell script?

Tags:

shell

filesize

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?

like image 513
seriousdev Avatar asked Dec 21 '10 09:12

seriousdev


People also ask

How do I find the size of a file in Shell?

Getting file size using find command find "/etc/passwd" -printf "%s" find "/etc/passwd" -printf "%s\n" fileName="/etc/hosts" mysize=$(find "$fileName" -printf "%s") printf "File %s size = %d\n" $fileName $mysize echo "${fileName} size is ${mysize} bytes."

How do I find the exact file size in Linux?

The best Linux command to check file size is using du command. What we need is to open the terminal and type du -sh file name in the prompt. The file size will be listed on the first column. The size will be displayed in Human Readable Format.

How do I check the size of a file in Unix?

don't worry we have a got a UNIX command to do that for you and command is "df" which displays the size of the file system in UNIX. You can run "df" UNIX command with the current directory or any specified directory.

How do I check the size of a file in Ubuntu terminal?

Use ls The -l option tells ls to show various metadata about the file, including file size. Without this option, ls only shows filenames. The -h option tells ls to show human-friendly units such as M for megabytes, G for gigabytes, etc.


1 Answers

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.

$ 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 
like image 144
codaddict Avatar answered Oct 01 '22 19:10

codaddict