linux批量复制文件到多个文件


第一种方法:

将source.txt 文件复制4份

#!/bin/bash
i=0
while [ $i -le 4 ]
do
    cp -vf source.txt  $i.txt
    let i+=1
done

[kdb11new@host4 newtest]$ ll  
-rw-r--r-- 1 kdb11new dba 1048627 Jan 28 13:48 1.txt
-rw-r--r-- 1 kdb11new dba 1048627 Jan 28 13:48 2.txt
-rw-r--r-- 1 kdb11new dba 1048627 Jan 28 13:49 3.txt
-rw-r--r-- 1 kdb11new dba 1048627 Jan 28 13:49 4.txt
-rw-r--r-- 1 kdb11new dba 1048627 Jan 28 13:21 source.txt


第二种方法:

#!/bin/bash

echo "input your file name"
read  FILENAME 
echo "how many times you want copy?"  
read TIMES 
echo "your file name is ${FILENAME}, you want to copy ${TIMES} times."  
BASE=`echo ${FILENAME}|cut -d "." -f 1`
EXT=`echo ${FILENAME}|cut -d "." -f 2`  
for(( i=0;i<${TIMES};i++))
do
  echo "copy ${BASE}.${EXT} to ${BASE}$i.${EXT} ..."
  cp "${BASE}.${EXT}" "${BASE}$i.${EXT}"
done


[kdb11new@host4 shell]$ ./makefile 
input your file name
secueed.sh
how many times you want copy?
10
your file name is secueed.sh, you want to copy 10 times.
copy secueed.sh to secueed0.sh ...
copy secueed.sh to secueed1.sh ...
copy secueed.sh to secueed2.sh ...
copy secueed.sh to secueed3.sh ...
copy secueed.sh to secueed4.sh ...
copy secueed.sh to secueed5.sh ...
copy secueed.sh to secueed6.sh ...
copy secueed.sh to secueed7.sh ...
copy secueed.sh to secueed8.sh ...
copy secueed.sh to secueed9.sh ...

[kdb11new@host4 shell]$ ll
total 52
-rwxr-xr-x 1 kdb11new dba 424 Jan 28 10:07 makefile
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:08 secueed0.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:08 secueed1.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:08 secueed2.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:08 secueed3.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:08 secueed4.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:08 secueed5.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:08 secueed6.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:08 secueed7.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:08 secueed8.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:08 secueed9.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 10:19 secueed.sh


第三种方法:

#!/bin/bash

echo "input your file name"
read  FILENAME
echo "how many times you want copy?" 
read TIMES 
echo "your file name is ${FILENAME}, you want to copy ${TIMES} times."

#find . and cut the left part of the file name using ##
EXT=${FILENAME##*.}
#find . and cut the right part of the file name using %
BASE=${FILENAME%.*}
echo "base:$BASE"
echo "ext:$EXT"

for(( i=0;i<${TIMES};i++))
do
 echo "copy ${BASE}.${EXT} to ${BASE}$i.${EXT} ..."
 cp "${BASE}.${EXT}" "${BASE}$i.${EXT}"
done


[kdb11new@host4 shell]$ ./bachfile 
input your file name
 secueed.sh
how many times you want copy?
11
your file name is secueed.sh, you want to copy 11 times.
base:secueed
ext:sh
copy secueed.sh to secueed0.sh ...
copy secueed.sh to secueed1.sh ...
copy secueed.sh to secueed2.sh ...
copy secueed.sh to secueed3.sh ...
copy secueed.sh to secueed4.sh ...
copy secueed.sh to secueed5.sh ...
copy secueed.sh to secueed6.sh ...
copy secueed.sh to secueed7.sh ...
copy secueed.sh to secueed8.sh ...
copy secueed.sh to secueed9.sh ...
copy secueed.sh to secueed10.sh ...
[kdb11new@host4 shell]$ ll
total 56
-rwxr-xr-x 1 kdb11new dba 491 Jan 28 10:00 bachfile
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:14 secueed0.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:14 secueed10.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:14 secueed1.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:14 secueed2.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:14 secueed3.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:14 secueed4.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:14 secueed5.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:14 secueed6.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:14 secueed7.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:14 secueed8.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 16:14 secueed9.sh
-rwxr-xr-x 1 kdb11new dba 110 Jan 28 10:19 secueed.sh







请使用浏览器的分享功能分享到微信等