简体   繁体   中英

How to download a file with wget and save it according to the http-reported filename?

When you request a file with wget and that file is being served by some dynamic page (eg php), wget will try to use the path to that dynamic page (usually looking as if an angry child got hold of your keyboard: index.php?a8s7df6a8s=d6fa8sd6f90v78wg&l45i87ylqwiu45h=j76h2g461k326v ).

However, these pages usually send an HTTP header with the file so that user agents can display a sensible file name. How do I get wget to listen to that and use it (instead of the url) to determine the name under which to save the file?

I found that a way to do this was to use the --server-response flag with --spider and invoke wget twice (there is certainly room for improvement, there!)

Assume the url to be in $link :

 wget --quiet --server-response --spider -O /dev/null -- "$link" 2>&1 \
 | sed -n 's/^.*filename=\([^;]*\)\(;.*\)\?$/\1/p' \
 | while read name; do
   wget -O "$name" -- "$link"
   break
 done

Seems to work like a charm for me.

Possibly, there is a direct way, though. This creates (completely unnecessarily) two connections to the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM