如何将url与文件中的文件名组合在一起

时间:2016-04-19 12:14:36

标签: bash

文件名(文件名:listing.txt),文件名为其内容:

ace.pdf
123.pdf
hello.pdf

想要从网址http://www.myurl.com/

下载这些文件

在bash中,尝试将这些合并在一起并使用wget下载文件,例如:

http://www.myurl.com/ace.pdf
http://www.myurl.com/123.pdf
http://www.myurl.com/hello.pdf

尝试了以下各种变体,但没有成功:

for i in $(cat listing.txt); do wget http://www.myurl.com/$i; done

2 个答案:

答案 0 :(得分:2)

无需使用cat并循环播放。您可以使用xargs

xargs -I {} wget http://www.myurl.com/{} < listing.txt

答案 1 :(得分:1)

实际上,wget有可以避免循环和选项的选项。外部程序完全。

   -i file
   --input-file=file
       Read URLs from a local or external file.  If - is specified as file, URLs are read from the standard input.  (Use ./- to read from a file literally named -.)

       If this function is used, no URLs need be present on the command line.  If there are URLs both on the command line and in an input file, those on the command lines will be the first ones to be retrieved.  If --force-html
       is not specified, then file should consist of a series of URLs, one per line.

       However, if you specify --force-html, the document will be regarded as html.  In that case you may have problems with relative links, which you can solve either by adding "<base href="url">" to the documents or by
       specifying --base=url on the command line.

       If the file is an external one, the document will be automatically treated as html if the Content-Type matches text/html.  Furthermore, the file's location will be implicitly used as base href if none was specified.

   -B URL
   --base=URL
       Resolves relative links using URL as the point of reference, when reading links from an HTML file specified via the -i/--input-file option (together with --force-html, or when the input file was fetched remotely from a
       server describing it as HTML). This is equivalent to the presence of a "BASE" tag in the HTML input file, with URL as the value for the "href" attribute.

       For instance, if you specify http://foo/bar/a.html for URL, and Wget reads ../baz/b.html from the input file, it would be resolved to http://foo/baz/b.html.

因此,

$ cat listing.txt
ace.pdf
123.pdf
hello.pdf

$ wget -B http://www.myurl.com/ -i listing.txt

这将下载所有3个文件。