我有一个包含两列的文本文件:第一列是要保存的名称,第二列是资源的 url 地址。
10000899567110806314.jpg 'http://lifestyle.inquirer.net/files/2018/07/t0724cheekee-marcopolo_1-e1532358505274-620x298.jpg'
10001149035013559957.jpg 'https://www.politico.eu/wp-content/uploads/2018/07/GettyImages-1004567890.jpg'
10001268622353586394.jpg 'http://www.channelnewsasia.com/image/10549912/16x9/991/529/a7afd249388308118058689b0060a978/Zv/tour-de-france-5.jpg'
10001360495981714191.jpg 'https://media.breitbart.com/media/2018/07/Dan-Coats.jpg'
Run Code Online (Sandbox Code Playgroud)
该文件包含数千行,因此我想要一种快速下载和重命名这些图像的方法。
我阅读了多篇关于 SO 的文章并提出了这个解决方案:
cat list.txt | xargs -n 1 -P 4 -d '\n' wget -O
Run Code Online (Sandbox Code Playgroud)
它用于xargs并行下载。我想使用wgetwith-O选项来重命名下载的文件。当我运行单个wget命令时,效果很好。例子:
wget -O 10000899567110806314.jpg 'http://lifestyle.inquirer.net/files/2018/07/t0724cheekee-marcopolo_1-e1532358505274-620x298.jpg'
Run Code Online (Sandbox Code Playgroud)
但是当使用 xargs 运行命令并行下载时,出现以下错误:
Try `wget --help' for more options.
wget: missing URL
Usage: wget [OPTION]... [URL]...
Run Code Online (Sandbox Code Playgroud)
如果我生成一个仅包含(单列)换行符分隔 url 的文件并运行以下命令,则效果很好。
cat list.txt | xargs -n 1 -P 4 -d '\n' wget
Run Code Online (Sandbox Code Playgroud)
但是,我不想先下载文件,然后再执行重命名操作。
您收到的错误是因为您只传递一个参数-n 1才能使其工作,您需要传递 2 个参数,请尝试以下操作:
cat list.txt | xargs -n 2 -P 4 wget -O
Run Code Online (Sandbox Code Playgroud)
要使用整行作为参数,如 @PesaThe 建议,您可以使用 option -L 1,例如:
xargs < list.txt -P 4 -L 1 wget -O
Run Code Online (Sandbox Code Playgroud)
来自男人:
-L number
Call utility for every number non-empty lines read.
A line ending with a space continues to the next non-empty line.
If EOF is reached and fewer lines have been read than number then utility
will be called with the available lines. The -L and -n options are
mutually-exclusive; the last one given will be used.
Run Code Online (Sandbox Code Playgroud)