业务需求:需要把一个目录下的1000多万个文件迁移到远程机器
思路:用wget来把文件一个一个的迁移过去,因为文件数量比较大,如果一下在循环操作,会非常慢。所以分批操作,采用化整为零的方法。
#! /bin/sh
home=/usr/local/www/skate/image63delback
cd $home
if [ `pwd` == $home ];then
a="1 1000000 2000000 3000000 4000000 5000000 6000000 7000000 8000000 9000000"
for b in $a
do
c=`expr $b + 100000`
for loop in `sed -n "$b,$c"p $1`
do
path=`echo $loop | awk -F "/" ‘{print $1"/"$2"/"$3"/"$4}‘`
mkdir -p $path
/usr/bin/wget http://172.16.111.163/$loop -P $path
echo $loop >> $1.log
done
done
fi需求2:需要把A目录下的1000多万个小文件分批迁移到B目录,每次1000个,30分钟一次,mv之前需确认B目录为空。
思路:使用python的shutil模块,也用shell,大概思路与上文一般。
# -*- coding: utf-8 -*-
import os
import shutil
def Test1(rootDir):
list_dirs = os.walk(rootDir)
count=0
for root, dirs, files in list_dirs:
for d in dirs:
print os.path.join(root, d)
if os.listdir("/data/file/S10032666/"):
print "目录非空,请清空文件。"
return 0
else:
for f in files:
if count < 1000:
count=count +1
f = os.path.join(root, f)
shutil.move(f,"/data/file/S10032666/")
print os.path.join(root, f)
else:
return 0
Test1("/data/S10032666_bak/")本文出自 “创者思” 博客,请务必保留此出处http://strongit.blog.51cto.com/10020534/1812396
原文地址:http://strongit.blog.51cto.com/10020534/1812396