Created
November 13, 2013 19:57
-
-
Save hrwgc/7455343 to your computer and use it in GitHub Desktop.
bash wget - check if file exists at url before downloading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# simple function to check http response code before downloading a remote file | |
# example usage: | |
# if `validate_url $url >/dev/null`; then dosomething; else echo "does not exist"; fi | |
function validate_url(){ | |
if [[ `wget -S --spider $1 2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then echo "true"; fi | |
} |
hello Team,
Can you tell some solution for random file.
we need to download file daily from https url which is having random name for current data and time .
Starting name is fix and file last part is daily changed due to date and time.
current we use for loop for same but not success output always.
thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, and thanks for this,
Anyway, I simplified the code before using it, there is no need to grep since wget returns 0 (OK) if file exists and there is no error, like authentication error for example:
And if you want no output from wget (always hide output instead of putting
>/dev/null
when callingvalidate_url
):And call it with:
And I though I had to do some extra dev since I need to give a user and password and another parameter but it works too with for example (put all parameter inside "" is important else $1 in
wget -S --spider $1
will only be the part before first space, URL then in example):