I currently working on a script that updates a file. The file has several versions. The important ones have static links. These get redirected to the actual file which then gets downloaded using wget. I have figured out that wget has a flag whcih prints the headers recived. There is a list of Locations. The last location meantoned in the header is the actual URL. I need to get that!My idea was to use wget -S to get the header (I need another flag that prevents the file from downloading and creating). Then use a pipe to parse the lines and catch the last line containing location. I guess this could be realized by using grep -l -i location: | tail -l. Then I should be left with a single line that can be easily parsed.So the command would look something like this:# The -??? flag is the one that prevents the file from downloading. (I don't know it)Location=$(wget -S -??? $URL | grep -l -i location: | tail -l)My question what flag do I have to use to not download the file with wget or is there another way/command to accomplish this?
How to figure out where a link gets redirected
shell script;wget;curl;http
What you want is a HEAD request, but wget does not support it; curl does.Your distribution most probably has curl in repositories.curl -s -I $URL -L | awk '/Location: (.*)/ {print $2}' | tail -n 1$ URL=http://unix.stackexchange.com/questions/89282/$ curl -s -I $URL | awk '/Location: (.*)/ {print $2}' | tail -n 1/questions/89282/how-to-figure-out-where-a-link-gets-redirected$ _Here: -s prevents curl from showing a progress bar;-I makes curl issue a HEAD request;-L makes curl follow redirects (thanks @brianstone), you may want or not want to include this, depending on which redirect headers you want to track;the awk script prints the matched expression in parens, just the local part of the URI.