Buzzkillionair
Buzzkillionair

Reputation: 379

Find file size before downloading Image with Scraper wget

I am trying to get an image/video size before my wget actually downloads it. is there a way to get the size and save it to a variable?

I tried to find this online, however, I was unable to find any solutions.

Upvotes: 1

Views: 436

Answers (1)

Max Voisard
Max Voisard

Reputation: 1942

Use the --spider option and then save the output to a text file:

$ wget --spider https://www.google.com/image.jpg > output.txt
$ wget --spider https://www.google.com/video.mp4 > output.txt

Example output:

Spider mode enabled. Check if remote file exists.
--2016-09-16 14:23:42--  http://www.bbc.co.uk/
Resolving www.bbc.co.uk (www.bbc.co.uk)... 212.58.244.67, 212.58.246.91
Connecting to www.bbc.co.uk (www.bbc.co.uk)|212.58.244.67|:80... connected.
HTTP request sent, awaiting response...
  HTTP/1.1 200 OK
  Server: nginx
  Content-Type: text/html; charset=utf-8
  ...
Length: 171933 (168K) [text/html]  <---------- Right here is the file size
Remote file exists and could contain further links,
but recursion is disabled -- not retrieving.

Then using awk to read the 2nd word of the 10th line to get the file size:

$ awk 'fileSize==10 {print $2}' output.txt

Upvotes: 1

Related Questions