网站图片文件按规则批量转移_2_代码实现

#!/bin/sh

#初始化目录
if [ ! -d "/web/www/item_images/2004" ];
then
mkdir /web/www/item_images/2004
fi
if [ ! -d "/web/www/item_images/2005" ];
then
mkdir /web/www/item_images/2005
fi
if [ ! -d "/web/www/item_images/2006" ];
then
mkdir /web/www/item_images/2006
fi
if [ ! -d "/web/www/item_images/2007" ];
then
mkdir /web/www/item_images/2007
fi
if [ ! -d "/web/www/item_images/2008" ];
then
mkdir /web/www/item_images/2008
fi
if [ ! -d "/web/www/item_images/2009" ];
then
mkdir /web/www/item_images/2009
fi

#基本目录
basedir="/web/www/item_images"
#工作目录为00-99
#workdir=$(ls $basedir | egrep '^[0-9]{2}$')
workdir=$(ls -d $basedir/* | egrep '/[0-9][0-9]$')
#遍历并拷贝文件
for file in $workdir
do
#如果是目录存在则处理
if [ -d $file ];
then
#处理2004年的item images,用正则表达式表达数值范围,好像是有点笨,不过很好的解决了我的问题,正则表达式代表文件名称为100005-101242之间的,则拷贝到对应的目录
filelist2004=$(ls $file | egrep '(10000[5-9]|1000[1-9][0-9]|100[1-9][0-9]{2}|101[0-1][0-9]{2}|1012[0-3][0-9]|10124[0-2])')
for file2004 in $filelist2004
do
#目标目录
tail2004=$(basename $file)
head2004=$(dirname $file)
yyyy2004="/2004/"
targetpath2004=$(echo $head2004$yyyy2004$tail2004)
#目标目录不存则创建
if [ ! -d "$targetpath2004" ];
then
mkdir $targetpath2004
fi

#源文件全路径名
srcpath2004=$(echo $file"/"$file2004)
#拷贝文件
#cp srcpath2004 $targetpath2004
#echo "$srcpath2004 -> $targetpath2004/$file2004" >>testlog2004.txt
print "$srcpath2004 -> $targetpath2004/$file2004" | tee –a testlog2004.txt
done

#处理2005年的item images
filelist2005=$(ls $file | egrep '(10124[3-9]|1012[5-9][0-9]|101[3-9][0-9]{2}|102[0-9]{3}|103[0-1][0-9]{2}|1032[0-5][0-9]|10326[0-4])')
for file2005 in $filelist2005
do
#目标目录
tail2005=$(basename $file)
head2005=$(dirname $file)
yyyy2005="/2005/"
targetpath2005=$(echo $head2005$yyyy2005$tail2005)
#目标目录不存则创建
if [ ! -d "$targetpath2005" ];
then
mkdir $targetpath2005
fi

#源文件全路径名
srcpath2005=$(echo $file"/"$file2005)
#拷贝文件
#cp srcpath2005 $targetpath2005
#echo "$srcpath2005 -> $targetpath2005/$file2005" >>testlog2005.txt
print "$srcpath2005 -> $targetpath2005/$file2005" | tee –a testlog2005.txt
done

#处理2006年的item images
filelist2006=$(ls $file | egrep '(10326[7-9]|1032[7-9][0-9]|103[3-9][0-9]{2}|104[0-9]{3}|105[0-2][0-9]{2}|1053[0-7][0-9]|10538[0-4])')
for file2006 in $filelist2006
do
#目标目录
tail2006=$(basename $file)
head2006=$(dirname $file)
yyyy2006="/2006/"
targetpath2006=$(echo $head2006$yyyy2006$tail2006)
#目标目录不存则创建
if [ ! -d "$targetpath2006" ];
then
mkdir $targetpath2006
fi

#源文件全路径名
srcpath2006=$(echo $file"/"$file2006)
#拷贝文件
#cp srcpath2006 $targetpath2006
#echo "$srcpath2006 -> $targetpath2006/$file2006" >>testlog2006.txt
print "$srcpath2006 -> $targetpath2006/$file2006" | tee –a testlog2006.txt
done

#处理2007年的item images
filelist2007=$(ls $file | egrep '(10538[5-9]|10539[0-9]|105[4-9][0-9]{2}|10[6-9][0-9]{3}|108[0-3][0-9]{2}|10840[0-9]|10841[0-4])')
for file2007 in $filelist2007
do
#目标目录
tail2007=$(basename $file)
head2007=$(dirname $file)
yyyy2007="/2007/"
targetpath2007=$(echo $head2007$yyyy2007$tail2007)
#目标目录不存则创建
if [ ! -d "$targetpath2007" ];
then
mkdir $targetpath2007
fi

#源文件全路径名
srcpath2007=$(echo $file"/"$file2007)
#拷贝文件
#cp srcpath2007 $targetpath2007
print "$srcpath2007 -> $targetpath2007/$file2007" | tee –a testlog2007.txt
#echo "$srcpath2007 -> $targetpath2007/$file2007" >>testlog2007.txt
done

#处理2008年的item images
filelist2008=$(ls $file | egrep '(10841[5-9]|1084[2-9][0-9]|108[5-9][0-9]{2}|109[0-9]{3}|11[0-1][0-9]{3}|112[0-5][0-9]{2}|1126[0-4][0-9]|11265[0-2])')
for file2008 in $filelist2008
do
#目标目录
tail2008=$(basename $file)
head2008=$(dirname $file)
yyyy2008="/2008/"
targetpath2008=$(echo $head2008$yyyy2008$tail2008)
#目标目录不存则创建
if [ ! -d "$targetpath2008" ];
then
mkdir $targetpath2008
fi

#源文件全路径名
srcpath2008=$(echo $file"/"$file2008)
#拷贝文件
#cp srcpath2008 $targetpath2008
print "$srcpath2009 -> $targetpath2008/$file2008" | tee –a testlog2008.txt
#echo "$srcpath2008 -> $targetpath2008/$file2008" >>testlog2008.txt
done

#处理2009年的item images
filelist2009=$(ls $file | egrep '(11265[4-9]|1126[6-9][0-9]|112[7-9][0-9]{2}|11[3-4][0-9]{3}|115[0-7][0-9]{2}|11580[0-9]|11581[0-8])')
for file2009 in $filelist2009
do
#目标目录
tail2009=$(basename $file)
head2009=$(dirname $file)
yyyy2009="/2009/"
targetpath2009=$(echo $head2009$yyyy2009$tail2009)
#目标目录不存则创建
if [ ! -d "$targetpath2009" ];
then
mkdir $targetpath2009
fi

#源文件全路径名
srcpath2009=$(echo $file"/"$file2009)
#拷贝文件
#cp srcpath2009 $targetpath2009
print "$srcpath2009 -> $targetpath2009/$file2009" | tee –a testlog2009.txt
#echo "$srcpath2009 -> $targetpath2009/$file2009" >>testlog2009.txt
done
fi
done
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值