工作中有遇到要把一个文件拷贝到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复制文件到多个目录下及强制覆盖的更多相关文章

  1. cp - 复制文件和目录

    总览 cp [选项] 文件路径 cp [选项] 文件...目录 POSIX 选项: [-fipRr] GNU 参数(最短形式): [-abdfilprsuvxPR] [-S SUFFIX] [-V { ...

  2. Java_io_02_从一个目录拷贝文件到另一个目录下

    java从一个目录拷贝文件到另一个目录下   http://www.cnblogs.com/langtianya/p/4857524.html ** * 复制单个文件 * @param oldPath ...

  3. Java中删除文件、删除目录及目录下所有文件(转)

    原文链接:Java中删除文件.删除目录及目录下所有文件 知识点:File.delete()用于删除“某个文件或者空目录”!所以要删除某个目录及其中的所有文件和子目录,要进行递归删除,具体代码示例如下: ...

  4. 关于DLL文件和EXE文件不在同一目录下的设置【转】

    https://www.cnblogs.com/chaosimple/archive/2012/08/13/2636181.html 关于DLL文件和EXE文件不在同一目录下的设置 在开发程序结束后, ...

  5. Maven项目如何将自定义文件添加到META-INF目录下

    Maven项目如何将自定义文件添加到META-INF目录下 学习了:https://blog.csdn.net/yangjiegreat/article/details/78698655 <bu ...

  6. C# 将引用的DLL文件放到指定的目录下

    原文:C# 将引用的DLL文件放到指定的目录下 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sweety820/article/details/2 ...

  7. java中把文件拷贝到指定目录下最简单几种方法

    java中把文件拷贝到指定目录下最简单几种方法   String savePath = "D:/file";// 文件保存到d盘的file目录下 File savefile = n ...

  8. cp 复制文件或目录

    1. 命令功能 cp --copy files and directories.复制文件或目录. 2. 语法格式 cp  [option]  source des cp  [option]  sour ...

  9. Java 复制一个文件到另外一个目录下

    因为项目部署在jboss上面,在上传一些图片的时候,把他上传到当前项目的下,比如:(这里是以Windows服务器为例的,当然linux也是一样的) D:\jboss-eap-6.4\domain\se ...

随机推荐

  1. EDP转接IC NCS8805:RGB/LVDS转EDP芯片,带Scaler

    RGB/LVDS-to-eDP Converter w/ Scaler1 FeaturesEmbedded-DisplayPort (eDP) Output1/2/4-lane eDP @ 1.62/ ...

  2. 鼠标滚动:mousewheel事件在Firefox采用DOMMouseScroll事件的统一处理

    这是一个小事件,但当下的WEB应用交互非常丰富,判断鼠标的滚动来执行相应的操作是比较常见的.我用Chrome/IE/Firefox/Opera 4种浏览器做测试,发现只有firefox的处理方法有很大 ...

  3. xshell配色Solarized Dark

    转自:xshell配色Solarized Dark [Solarized_Dark] text(bold)= magenta(bold)=6c71c4 text= white(bold)=fdf6e3 ...

  4. 转载---关于Spring的69个面试问答

    链接:http://www.importnew.com/11657.html 目录 Spring概述 依赖注入 Spring Beans Spring注解 Spring的对象访问 Spring面向切面 ...

  5. java复习(1)---java与C++区别

    [系列说明]java复习系列适宜有过java学习或C++基础或了解java初步知识的人阅读,目的是为了帮助学习过java但是好久没用已经遗忘了的童鞋快速捡起来.或者教给想快速学习java的童鞋如何应用 ...

  6. Android 性能优化——之图片的优化

    Android 性能优化——之图片的优化 在Android性能优化中,我们会发现占内存最大的和对性能影响最大的往往是图片资源,其次是控件资源.相对来说,其他的资源的影响会小一点.这里我就先对图片资源的 ...

  7. DirectFB学习笔记四

    本篇目的,实现按钮的点击事件捕获,也就是鼠标点击,如果点击在方框范围内,则响应,在方框外,则忽略. 由于鼠标移动和点击都会产生事件,因此,我们可以在鼠标移动的时候记录坐标,在点击时比较坐标是否在方框范 ...

  8. IOC容器的依赖注入

    1.依赖注入发生的时间 当Spring IoC容器完成了Bean定义资源的定位.载入和解析注册以后,IoC容器中已经管理类Bean定义的相关数据,但是此时IoC容器还没有对所管理的Bean进行依赖注入 ...

  9. python自动化测试应用-番外篇--接口测试2

    篇2                 book-python-auto-test-番外篇--接口测试2 --lamecho辣么丑 大家好! 我是lamecho(辣么丑),今天将继续上一篇python接 ...

  10. bootstrap快速入门笔记(一)

    一,头部基本格式:<head lang="en"> <meta charset="UTF-8"> <meta name=" ...