工作中有遇到要把一个文件拷贝到N个文件夹下,但是cp又没有这样的命令,怎么办,这时需要编写一个脚本,首先做实验如下:
[root@host1 ~]# mkdir test
[root@host1 ~]# cd test
[root@host1 test]# mkdir -p test_123 test_abc test_xyz testlog
[root@host1 test]# cd testlog/
[root@host1 testlog]# echo "Test log" > test.log
[root@host1 testlog]# cat cp.sh
#!/bin/bash
log=test.log
for dir in `find /root/test -name "test_*"`
do
cp $log $dir
done
[root@host1 testlog]# bash -x cp.sh
+ log=test.log
++ find /root/test -name 'test_*'
+ for dir in '`find /root/test -name "test_*"`'
+ cp test.log /root/test/test_xyz
+ for dir in '`find /root/test -name "test_*"`'
+ cp test.log /root/test/test_abc
+ for dir in '`find /root/test -name "test_*"`'
+ cp test.log /root/test/test_123
[root@host1 testlog]# cat /root/test/test_123/test.log
Test log
使用脚本写的命令,是可以强制覆盖原先有的文件的,而没有提示,我们验证下:
[root@host1 testlog]# echo "Test log once" > test.log
[root@host1 testlog]# bash cp.sh
[root@host1 testlog]# cat /root/test/test_123/test.log
Test log once
但是如果我们在命令行使用cp命令,则会提示是否需要覆盖:
[root@host1 testlog]# cp test.log /root/test/test_123/
cp:是否覆盖"/root/test/test_123/test.log"? y
解决这个问题的方法很简单,只需在前面加上"\":
[root@host1 testlog]# echo "Test log once again" > test.log
[root@host1 testlog]# \cp test.log /root/test/test_123/
[root@host1 testlog]#
[root@host1 testlog]# cat /root/test/test_123/test.log
Test log once again
这样就可以解决每次覆盖敲y的烦恼了,是不是很棒^_^
当然还可以取消别名或者修改别名,但是比较麻烦,用完之后还要恢复回来,如果忘记修改回来,对以后的工作可能会造成麻烦哦!
cp - 复制文件和目录
总览 cp [选项] 文件路径 cp [选项] 文件...目录 POSIX 选项: [-fipRr] GNU 参数(最短形式): [-abdfilprsuvxPR] [-S SUFFIX] [-V { ...
Java_io_02_从一个目录拷贝文件到另一个目录下
java从一个目录拷贝文件到另一个目录下 http://www.cnblogs.com/langtianya/p/4857524.html ** * 复制单个文件 * @param oldPath ...
Java中删除文件、删除目录及目录下所有文件(转)
原文链接:Java中删除文件.删除目录及目录下所有文件 知识点:File.delete()用于删除“某个文件或者空目录”!所以要删除某个目录及其中的所有文件和子目录,要进行递归删除,具体代码示例如下: ...
关于DLL文件和EXE文件不在同一目录下的设置【转】
https://www.cnblogs.com/chaosimple/archive/2012/08/13/2636181.html 关于DLL文件和EXE文件不在同一目录下的设置 在开发程序结束后, ...
Maven项目如何将自定义文件添加到META-INF目录下
Maven项目如何将自定义文件添加到META-INF目录下 学习了:https://blog.csdn.net/yangjiegreat/article/details/78698655
C# 将引用的DLL文件放到指定的目录下
原文:C# 将引用的DLL文件放到指定的目录下 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sweety820/article/details/2 ...
java中把文件拷贝到指定目录下最简单几种方法
java中把文件拷贝到指定目录下最简单几种方法 String savePath = "D:/file";// 文件保存到d盘的file目录下 File savefile = n ...
cp 复制文件或目录
1. 命令功能 cp --copy files and directories.复制文件或目录. 2. 语法格式 cp [option] source des cp [option] sour ...
Java 复制一个文件到另外一个目录下
因为项目部署在jboss上面,在上传一些图片的时候,把他上传到当前项目的下,比如:(这里是以Windows服务器为例的,当然linux也是一样的) D:\jboss-eap-6.4\domain\se ...
随机推荐
[译]C++, Java和C#的编译过程解析
1.1.1 摘要 我们知道计算机不能直接理解高级语言,它只能理解机器语言,所以我们必须要把高级语言翻译成机器语言,这样计算机才能执行高级语言编写的程序,在接下来的博文中,我们将介绍非托管和托管语音的编 ...
Kafka中的Message Delivary机制
学习Kafka的读书笔记,暂未把文章设为翻译类型,因为并非直译文档.水平有限,还请路过高手指正. <1> “最多(发送)一次”(At most once):消息可以丢失但绝不会重新发送:& ...
HTML DOM访问
访问 HTML 元素(节点) 访问 HTML 元素等同于访问节点 您能够以不同的方式来访问 HTML 元素: 通过使用 getElementById() 方法 通过使用 getElementsByTa ...
css3属性选择器总结
前两节介绍了css3属性选择器与css2属性选择器中: 包含字符串和以字符串选择器开头的选择器的比较. 全部属性选择器: 包含字符串讲解对比实例讲解链接: http://www.cnblogs.com ...
beta冲刺2
q前言:今天晚上大概把github的的相关东西弄上了.然后把之前做的一些修改什么的也上传上去了.目测之后的话就是在自己这边改完然后直接上github那边去复制粘贴替换掉了. 昨天的问题:github这 ...
shiro三连斩之第二斩(SSM)
在SSM框架中使用shiro.环境 使用idea工具. 最主要的大概是配置文件如何配置吧. 1配置maven依赖 <?xml version="1.0" encoding=& ...
python 发送163邮件
可能还需要使用 邮箱第三方客户端的授权码. 网易163免费邮箱相关服务器信息 from email import encoders from email.header import Header fr ...
rviz初接触2.0
用rviz之前需要安装arbotix 在catkin_ws/src中 git clone https://github.com/vanadiumlabs/arbotix_ros.git 回到catki ...
git 不能创建分支
git 不能创建分支,如下 fatal: cannot lock ref 'refs/heads/hotfix/aa': 'refs/heads/hotfix' exists; cannot crea ...