shell命令-将源目录下文件拷贝到目标目录

时间:2024-02-22 16:12:00
#!/bin/bash
function read_dir(){
for file in `ls $1` #``这是两个反引号,表示运行系统命令
do
 if [ -d $1"/"$file ] #注意此处之间一定要加上空格,否则会报错
 then
 	read_dir $1"/"$file $2
 else
	 str=$1
	 room=${str##*/}
	 str=${str%/*}
	 date=${str##*/}
	 dst=$2"/"$date"/"$room
	 `mkdir -p $dst`

	 for myfile in `ls $1`
	 do
	 	if [ "$myfile" == "$room"".chat" ]
	 	then
	 		`cp $1"/"$myfile $dst`
	 		echo $1"/"$myfile"------------->"$dst
	 	elif [ "$myfile" == "$room"".raw" ]
	 	then
	 		`cp $1"/"$myfile $dst`
	 		echo $1"/"$myfile"------------->"$dst
	 	elif [ "$myfile" == "presenters.js" ]
	 	then
	 		`cp $1"/"$myfile $dst`
	 		echo $1"/"$myfile"------------->"$dst
	 	fi
	 done
	 break
 fi
done
} 

starttime=$(date +%Y-%m-%d\ %H:%M:%S)

echo "开始拷贝文件:"$starttime
#读取第一个参数
read_dir $1 $2

ttime=`date +"%Y-%m-%d %H:%M:%S"`

echo "文件拷贝完成:"$ttime

  shell基本语法:https://www.cnblogs.com/tongye/p/9707590.html