Linux环境下文件批量复制脚本

简介

在做项目的时候,有一种需求,class文件只能先以普通用户身份用ftp复制到linux,然后用shell工具以root用户将ftp复制来的文件复制到对应的项目目录,很麻烦,所以写了一段脚本完成这些任务。

功能

只需要把要复制的文件放到同一个临时目录当中,然后修改脚本文件,sourcePath表示临时目录,targetPath表示项目存放的目录

实例

假设:需要替换项目中的多个class,xml等文件,a.class、b.xml、c.class.....,并且存放在/home/mote/temp/文件夹中,web项目在Linux中的路径  /mnt/local/tomcat/webapps/project_a/

步骤:

第一种

1、修改脚本中的“sourcePath”为:/home/mote/temp/

2、修改脚本中的“targetPath”为:/mnt/local/tomcat/webapps/project_a/

3、执行脚本文件  sh shell_name.sh(连续按两次回车键)

第二种

1、执行脚本文件  sh shell_name.sh

2、输入新文件的路径:/home/mote/temp/   然后回车(注意:最后边加上“/”)

3、输入项目的目录,最好能定位到最小范围:/mnt/local/tomcat/webapps/project_a/  (注意:最后边加上“/”)

4、回车

备注:在替换文件的时候会出现覆盖提示,输入“y”,然后回车即可

脚本

#! /bin/bash

function read_dir(){

	#新文件的存放目录
	sourcePath="/home/qqt/mote/files/"
	#项目目录,最好是webapps的下下级目录
	targetPath="/mnt/local/apache-tomcat-7.0.67-sta/webapps/ELSServer_STA/"
	#全新文件提示
	new_files=""
	echo "默认源文件目录为:$sourcePath"
	echo "默认应用目录为:$targetPath"
	echo "请输入源文件路径(直接点击回车则使用默认目录)"
	read source_input
	echo "请输入应用目录(直接点击回车则使用默认目录)"
	read target_input
	
	if test -n "$source_input" 
	then
		if test -n "$target_input" 
		then
			for file in `ls $source_input`
			do
				if test -f $source_input${file} 
				then
					bl=$(find $target_input -type f -name $file)
					path_sta=${bl%/*}
					
					if [ ! ${#path_sta} -gt 1 ]
					then
						new_files="${new_files},   ${file}"
					else
					cp -i -r $source_input$file  $path_sta/$file
					fi
					fi
			done
		else
			for file in `ls $source_input`
			do
				if test -f $source_input${file} 
				then
					bl=$(find $targetPath -type f -name $file)
					path_sta=${bl%/*}
					
							if [ ! ${#path_sta} -gt 1 ]
					then
						new_files="${new_files},   ${file}"
					else
					cp -i -r $source_input$file  $path_sta/$file
					fi
					
					
				fi
			done
		fi
	else
		if test -n "$target_input" 
		then
	
			for file in `ls $sourcePath`
			do
				if test -f $sourcePath${file} 
				then
					bl=$(find $target_input -type f -name $file)
					path_sta=${bl%/*}
						if [ ! ${#path_sta} -gt 1 ]
					then
						new_files="${new_files},   ${file}"
					else
					cp -i -r $sourcePath$file  $path_sta/$file
					fi
				fi
			done
		else
			for file in `ls $sourcePath`
			do
				if test -f $sourcePath${file} 
				then

					bl=$(find $targetPath -type f -name $file)
					path_sta=${bl%/*}
					if [ ! ${#path_sta} -gt 1 ]
					then
						new_files="${new_files},   ${file}"
					else
					cp -i -r $sourcePath$file  $path_sta/$file
					fi
				fi
			done
		fi
	fi
	
	if [ ${#new_files} -gt 1 ]
	then
		echo "项目中不能存在以下文件,请手动复制!"
		echo ${new_files}
	fi
}

read_dir







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值