shell脚本批量下载文件且保留原始路径

发布时间:2020-01-27编辑:脚本学堂
用shell脚本批量下载文件,并保留文件的原始路径,shell与wget命令结合实现文件下载的例子。

有如下url.txt列表:
 

http://www.xxx.com/static/superplus/img/logo_white_ee663702.png
http://www.xxx.com/static/superplus/img/logo_white_ee663703.png
http://www.xxx.com/static/superplus/img/logo_white_ee663701.png
http://www.xxx.com/static/superplus/img/logo_white_ee663704.png
http://www.xxx.com/static/superplus/img/logo_white_ee663705.png
http://www.xxx.com/static/superplus/img/logo_white_ee663706.png

如果手工下载这些图片,并保存在各自的文件夹下,会比较麻烦,费时费力,最好的办法是用shell/ target=_blank class=infotextkey>shell脚本,结合linuxjishu/14085.html target=_blank class=infotextkey>wget命令,实现批量下载。

shell脚本:download.sh
 

复制代码 代码示例:

#!/bin/bash
# desc: download resource

mydir=`pwd`

while read line
do
{
    if [ -n "$line" ]
    then
        cd $mydir
        url=$(echo "$line" | tr -d 'r')
        picdir=$(echo $url | sed -r 's/http:////g')
        picname=$(echo ${picdir##*/})
        picpath=$(echo ${picdir%/*})
        mkdir -p $picpath
        cd $picpath
        wget -O $picname `echo $url`
    fi
}
done < $1
exit 0

注意:
1、为了去掉文本文件中行末的换行符,要进行删除:
 

tr -d 'r'

2、取资源名:
 

${picdir##*/}

3、取资源路径:
 

${picdir%/*}

运行:
 

sh download.sh url.txt