使用 wget 从列表中下载文件

no *_*ame 1 command-line scripts

我有一个文件,其中包含这样的下载链接:

Google.com/image2
Google.com/image3
Google.com/image4
Google.com/image5
Google.com/image6
Run Code Online (Sandbox Code Playgroud)

我想使用脚本下载所有这些。如果名称以's'开头,则将此文件下载到s目录,如果是b,则将其移动到b目录...

Rav*_*ina 11

下载所有文件,然后使用 shell globs 移动它们:

#!/bin/bash
wget -i /path/to/download_list
mv s* ./s/
mv b* ./b/
Run Code Online (Sandbox Code Playgroud)
  • -i:从本地或外部文件中读取 URL。

您可能会收到警告:

mv: cannot move 's' to a subdirectory of itself.
Run Code Online (Sandbox Code Playgroud)

没关系,你可以忽略它,或者find改用:

#!/bin/bash
wget -i /path/to/download_list
find -maxdepth 1 -iname "s*" -type f -exec mv "{}" ./s \;
find -maxdepth 1 -iname "b*" -type f -exec mv "{}" ./b \;
Run Code Online (Sandbox Code Playgroud)

通过for循环,您可以在所有字母上运行,脚本名称为script.sh

#!/bin/bash
wget -i /path/to/download_list
mkdir -p {a..z}
for l in {a..z};
do
    find -maxdepth 1 -type f -iname "${l}*" -not -iname script.sh -exec mv "{}" "./${l}" \;
done
Run Code Online (Sandbox Code Playgroud)


pLu*_*umo 5

对@Ravexina 很好的回答的补充。

没有循环的解决方案:

wget -i /path/to/download_list
mkdir -p {a..z}
# mv the files with rename tool
rename 's/^((.).+)$/$2\/$1/' *
# clean up empty directories
find . -maxdepth 1 -name '[a-z]' -type d -empty -delete
Run Code Online (Sandbox Code Playgroud)