wget 下载列表到指定目录

wget 下载列表到指定目录

我想创建一个简短但实用的脚本,让 wget 使用 .list 文件。问题是我想设置这些文件所在的目录。

例子:

file: url.list
[group 1]
http://www.somehost.com/files/tool.7z
http://www.someotherhost.com/files/icon36.png

[group 2]
http://www.idunno.net/other-tool.tar.gz
http://265.265.265.265/config.ini
http://www.myownsite.com/tools/script-to-run-tool.cmd
eof

([group 1] 和 [group 2] 只是为了方便阅读而放在这里,它们不在我的真实列表文件中)(是的,我知道 265 不是真实的,这就是为什么它是一个例子)

命令(当前使用无法解析文件夹) wget --continue --timestamping --content-disposition -i url.list

当然,这会将所有 5 个文件下载到同一目录。我的问题是,有没有办法告诉 wget 对组 1 和组 2 使用不同的文件夹,就我而言,我希望它能抓取我在工作中使用的几个工具,我在 Windows 中有一个单独的脚本,它会创建一个 winpe usb 密钥并将这些目录中的所有工具注入到该密钥中。

所以我的最终问题是,这可以非常简单地完成吗,还是需要我使用完整的 bash 脚本来抓取它们并创建文件夹让它们进入并将它们移动到那里?(在 wget 中使用 -o 拧紧我的时间戳,而时间戳是关键任务)

理论上,当这一切完成后,我希望得到一份(伪名称)的新副本:

tools/cool-tool/tool.7z
tools/cool-tool/icon36.png

tools/special-tool/other-tool.tar.gz
tools/special-tool/config.ini
tools/special-tool/script-to-run-tool.cmd

答案1

创建一个在同一行包含 URL 和目标目录的列表:

http://www.somehost.com/files/tool.7z tools/cool-tool/
http://www.someotherhost.com/files/icon36.png tools/cool-tool/
http://www.idunno.net/other-tool.tar.gz tools/special-tool/
http://265.265.265.265/config.ini tools/special-tool/
http://www.myownsite.com/tools/script-to-run-tool.cmd tools/special-tool/

然后,使用bash循环读取文件并将其提供给 wget:

while read url target; do 
  wget "$url" -P "$target";
done < url.list

答案2

你应该能够用 wget-P--directory-prefix选项做你想做的事

   -P prefix
   --directory-prefix=prefix
       Set directory prefix to prefix.  The directory prefix is the
       directory where all other files and subdirectories will be saved
       to, i.e. the top of the retrieval tree.  The default is . (the
       current directory).

还有许多其他选项可用于控制如何处理任何源目录组件,这些选项也可能有用,具体取决于您是下载单个文件还是使用递归 wget - 请参阅Directory Optionswget 手册页的部分。

答案3

我想到了这个:

wgetfiles.sh

filename="$1"

while read -r line
do
    if [[ "$line" =~ (https?|ftp)\:\/\/ ]]
        then
            wget "$line" -P "$currdir";
        else
            currdir="$line"
            if [ ! -d "$currdir" ]; then
                mkdir "$currdir"
            fi
    fi
done < "$filename"

对于任意名称的文本文件:

file.txt

Dir 1
http://www.somehost.com/files/tool.7z tools/cool-tool/
https://www.someotherhost.com/files/icon36.png tools/cool-tool/
Dir 2
http://www.idunno.net/other-tool.tar.gz tools/special-tool/
https://265.265.265.265/config.ini tools/special-tool/
Dir 3
ftp://www.myownsite.com/tools/script-to-run-tool.cmd tools/special-tool/

这允许使用任何包含目录名和多个链接的文本文件。如果目录不存在,它会创建目录。不会进行任何检查,因此请确保目录后跟链接,如图所示。

只是一点补充。此行将涵盖许多情况:

wget "$line" -e robots=off -r -l 1 -nd -nc --wait 1 −−random−wait -R html,"index.*"  -P "$currdir"

它将忽略robots.txt-e robots=off),如果链接指向目录,则递归到目录(-r -l 1),不会从站点下载目录层次结构 - 仅下载文件(-nd),不会下载已下载的文件(-nc),将随机等待下载下一个文件以进一步帮助确保不会被站点拒绝(--wait 1 −−random−wait),并且不会下载html文件或形式为index.*-R html,"index.*")的文件。

另外,创建目录层次结构如下Dir 1/subdir 1/sub-subdir 1

mkdir -p "$currdir"

相关内容