Skip to content

Instantly share code, notes, and snippets.

@chaimleib
Created October 22, 2014 04:55
Show Gist options
  • Save chaimleib/89f88f0918c801f8d614 to your computer and use it in GitHub Desktop.
Save chaimleib/89f88f0918c801f8d614 to your computer and use it in GitHub Desktop.
Gets the actual name of a remote file, then downloads to an identically-named file
#!/bin/bash
function getUriFilename() {
header="$(curl -sI "$1" | tr -d '\r')"
filename="$(echo "$header" | grep -o -E 'filename=.*$')"
if [[ -n "$filename" ]]; then
echo "${filename#filename=}"
return
fi
filename="$(echo "$header" | grep -o -E 'Location:.*$')"
if [[ -n "$filename" ]]; then
basename "${filename#Location\:}"
return
fi
return 1
}
url="http://www.vim.org/scripts/download_script.php?src_id=10872"
filename="$(getUriFilename $url)"
curl -L $url -o "$filename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment