Skip to content

Instantly share code, notes, and snippets.

@AndersDJohnson
Last active June 5, 2022 15:24
Show Gist options
  • Save AndersDJohnson/3068883 to your computer and use it in GitHub Desktop.
Save AndersDJohnson/3068883 to your computer and use it in GitHub Desktop.
shell get download content-length
#! /bin/bash
# get download file size
URL=$1
curl -sI "${URL}" | grep "Content-Length" | cut -d ' ' -f 2
@Podrepny
Copy link

Podrepny commented Jun 5, 2022

Not good way to get size "cut -d' ' -f2"
SIZE="$(curl --silent --location --head --insecure "${LINK}" | grep -i '^Content-Length' | cut -d' ' -f2)"
Because if you use this for variable is will contains additional characters like this:
SIZE=$'597622784\r'
Better user grep:
SIZE="$(curl --silent --location --head --insecure "${LINK}" | grep -oP '(?i)(?<=Content-Length:\s)\d{1,20}(?=\D)?')"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment