【问题标题】:Parallel download for a list of urls and renaming并行下载 url 列表和重命名
【发布时间】:2014-07-14 18:19:02
【问题描述】:

我有一个制表符分隔的 url 列表和目标文件名,urls_to_download.txt,例如:

first_file.jpg\thttps://www.google.co.il/images/srpr/logo11w.png
/subdir_1/second_file.jpg\thttps://www.google.co.il/images/srpr/logo12w.png
...
last_file.jpg\thttps://www.google.co.il/images/srpr/logo99w.png

我想使用多个连接下载。

我可以这样做,例如:

cat urls_to_download.txt | xargs -n 1 -P 10 wget -nc

我的问题是,如何让文件具有我想要的新名称,因此输出目录将具有:

first_file.jpg
/subdir1/second_file.jpg
...
last_file.jpg

【问题讨论】:

    标签: linux wget


    【解决方案1】:

    我猜这样的东西应该适合你:

    #!/bin/bash                                                                                                                                                                       
    while read FILENAME URL; do
      wget -nc -O "$FILENAME" "$URL"
    done <input.txt
    

    其中 input.txt 是一个包含制表符分隔的文件/url 对的文件,每行一个。

    【讨论】:

      【解决方案2】:
      1. 请注意,文件中的文件名使用的是绝对路径。所以你最好把这些名字改成相对路径。

      2. 在shell中,只有用&amp;放一个进程后台才能让你的工作并行。

      例如,如果你想并行,你可以这样做:

      #!/bin/bash
      while read FILENAME URL
      do
          wget -nc -O "./$FILENAME" "$URL" &   # So `wget` runs in background
      done < input.txt
      

      注意:上面的脚本只是一个提示,如果input.txt 中有很多行,则会创建太多并行的wget 进程。有一些方法可以控制并行任务的数量,但是对于 shell 脚本来说这些方法或多或少有些复杂。

      一种非常简单的控制并行任务数量的方法,保证最多有20个wget进程。

      #!/bin/bash
      NUMBER=0
      while read FILENAME URL
      do
          wget -nc -O "./$FILENAME" "$URL" &   # So `wget` runs in background
          NUMBER=$((NUMBER + 1))
          if [ $NUMBER -gt 20 ]
          then
              wait   # wait all background process to finish
              NUMBER=0
          fi
      done < input.txt
      wait
      

      但是,这种方法非常简单,并不是控制并行任务数量的最有效和最准确的方法。

      【讨论】:

        【解决方案3】:

        试试这个命令同时下载你的文件:

        `cut -f 2 urls_to_download.txt | wget -i -;` 
        
        `cut -f 2 urls_to_download.txt | sed 's/.*\///' | while read f; do mv $f $(cut -f 1 urls_to_download.txt); done`
        

        我找不到使用wget 选项正确重命名文件的方法,您需要修改以确保该目录存在于mv 命令中。

        【讨论】:

          【解决方案4】:

          只需使用wget-x 选项:

          -x
          --force-目录
          -nd 的反面——创建目录层次结构,即使不会创建目录
          否则。例如。 wget -x http://fly.srk.fer.hr/robots.txt 将下载的文件保存到
          fly.srk.fer.hr/robots.txt。

          xargs -n 1 -P 10 wget -nc < urls_to_download.txt
          

          如果您的文件是制表符分隔的:

          xargs -n 1 -d $'\t' -P 10 wget -nc -x < urls_to_download.txt
          

          或者您可以将制表符转换为换行符:

          sed -e 's|\t|\n|g' urls_to_download.txt | xargs -n 1 -P 10 wget -nc -x
          

          【讨论】:

            猜你喜欢
            • 2017-06-30
            • 1970-01-01
            • 2011-12-29
            • 2022-05-31
            • 1970-01-01
            • 2017-07-31
            • 2016-11-14
            • 2019-07-13
            • 1970-01-01
            相关资源
            最近更新 更多