將指定目錄下指定數目的目錄分批移動到另一目錄

情景1:

現有一個目錄dir_origin,該目錄下有10000個子目錄。現需要將該目錄下的10000子目錄,100個子目錄爲一批,分別移動到指定目錄:1~100子目錄移動到dir1,101~102子目錄移動到dir2。只關注數目,不關注文件命名方式。

 

方法:

1. 用腳本創建100個格式化名稱的dirNUM

#!/bin/bash
# make sure you route of cp_$1 is right

function getfiles(){
    for ((i=1; i<=10; i ++))
    do
        `mkdir cp_$i`
    done
}

getfiles $1

2.分批移動目錄vim 

#!/bin/bash
# make sure  mv $1/$file cpdir/cp_$count_out have a right route


count_in=0
count_out=0

function getfiles(){
  for file in `ls $1`
  do
    if [ $count_in -eq 10 ]
    then
      ((count_out++))
      count_in=0
    fi
    mv $1/$file cpdir/cp_$count_out
    ((count_in++))
  done
}

getfiles $1

情景2 :

現有要求:

給定目錄名稱,建立給定名稱(如AAA)的目錄,並在該目錄下建立 指定數目的具有一定命名規則(如cp_[NUM])的目錄

從給定的目錄(BBB)中獲取1000個目錄,每100個目錄存儲在AAA/cp_[num] 目錄下:

 

創建目錄:

#!/bin/bash
# useage
# ./thisfilename.sh source
# NOTE:
#
#  ensure this .sh file is changed mod
#  source is which dir you want to operate

function getfiles(){
    # 建立指定目錄
    `mkdir cp_$1`
    for ((i=1; i<=12; i ++))
    do
        #循環建立目錄,i<=12,12指得是需要建立目錄的數目
        `mkdir cp_$1/cp_$i`
    done
}

getfiles $1

移動數據:

#!/bin/bash
# useage
# ./thisfilename.sh source
# NOTE:
#
#  ensure this .sh file is changed mod
#  source is which dir you want to operate
#  
count_in=0
count_out=1

function getfiles(){
  for file in `ls $1`
  do
    # 每100個複製一次
    if [ $count_in -eq 100 ]
    then
      # 複製100個之後,更換目錄
      ((count_out++))
      count_in=0
    fi
    mv $1/$file cpdir/cp_$count_out
    ((count_in++))
  done
}

getfiles $1

情景3:

當前目錄下目錄命名錯誤,需要批量重命名的。文件在當前目錄下運行: bash changename.sh ./ NEW_NAME

#!/bin/bash
# useage
# ./thisfilename.sh source
# NOTE:
#
#  ensure this .sh file is changed mod
#  source is which dir you want to operate
#  

function getfiles(){
  for file in `ls $1`
  do
    mv $file $2_$file
    #echo $file
  done
}

getfiles $1 $2

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章