Skip to content

Instantly share code, notes, and snippets.

@barbietunnie
Created December 15, 2024 11:26
Show Gist options
  • Save barbietunnie/24e0ddb8a08f8aa6393d2abf99d223f6 to your computer and use it in GitHub Desktop.
Save barbietunnie/24e0ddb8a08f8aa6393d2abf99d223f6 to your computer and use it in GitHub Desktop.
Get human-friendly file size of a remote file

Get human-friendly file size of a remote file

To determine the size of a file at a remote URL without downloading it, you can use an HTTP HEAD request. This type of request retrieves only the HTTP headers, which often include the Content-Length header that indicates the file size in bytes.

curl -sI https://datasets-documentation.s3.eu-west-3.amazonaws.com/uk-house-prices/postgres/uk_prices.sql.tar.gz | awk '/Content-Length/ { 
    size=$2; 
    split("B KB MB GB TB", unit); 
    for (i=1; size>=1024 && i<5; i++) size/=1024; 
    printf("%.2f %s\n", size, unit[i])
}'

Alternatively,

curl -sI "https://datasets-documentation.s3.eu-west-3.amazonaws.com/uk-house-prices/postgres/uk_prices.sql.tar.gz" | \
  grep -i content-length | \
  awk '{b=$2; k=b/1024; m=k/1024; g=m/1024; 
        if(g>=1)      printf "%.2f GB\n",g; 
        else if(m>=1) printf "%.2f MB\n",m;
        else if(k>=1) printf "%.2f KB\n",k;
        else          printf "%d B\n",b;}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment