日期:2014-05-16  浏览次数:20586 次

linux下实现文件转移的sh脚本

实现目标:CN目录下有若干文件夹,并且每个文件夹中有一系列以CN开头的子文件夹,现要求将CN开头的这些子文件夹实现按文件夹名进行组织存放,例如将/home/CN/temp/CN20081478023的文件夹存放到/home/CN/2008/1478/CN20081478023,则shell命令如下:

#!/bin/bash
# for
count=1
for file in ../CN/*
do
 if [ -d ../CN/${file} ]
 then
 for file1 in ${file}/CN*
 do
 if [ -d ${file1} ]
 then 
  string=${file1:11}
  a=${string:0:2}
  b=${string:2:4}
  c=${string:6:4}
  echo ${a} and ${b} and ${c}
  if [ ! -d ../CN/${b} ]
   then mkdir ../CN/${b}
  fi
  if [ ! -d ../CN/${b}/${c} ]
   then mkdir ../CN/${b}/${c}
  fi
  if [ ! -d ../CN/${b}/${c}/${string} ]
   then 
     cp -R ${file1} ../CN/${b}/${c}/${string}
     echo sucessfully copyed
     count=$[ $count + 1 ]
     echo ${count} has been copyed
  fi
 fi
 done
 fi
done >output.txt
?