问题:
我有一个包含下载链接的文件,如下所示:
Google.com/image2
Google.com/image3
Google.com/image4
Google.com/image5
Google.com/image6
我想使用脚本下载所有的文件。
回答 1:
下载所有文件,然后使用 shell 移动它们:
#!/bin/bash
wget -i/path/to/download_list
mv s*./s/
mv b*./b/
-i
: 从本地或者外部文件读取 url。
你可能会收到警告:
mv: cannot move 's' to a subdirectory of itself.
可以,你可以忽略它,或者使用 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 ;
使用 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
回答 2:
不用循环的解决方案:
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
回答 3:
直接下载到所需文件夹的解决方案:
# expand file to list and iterate
for path in $(<"/path/to/download_list"); do
# get file part of path
name=$(basename"$path")
# use first character of name as dir
dir=${name:0:1}
# create dir is not exist
mkdir -p"$dir"
# download path directly to dir
wget"$path" -P"$dir"
done
相关文章