工作中有遇到要把一个文件拷贝到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. 【Electron】Electron开发入门(二):创建项目Hello Word

    创建简单的Electron程序 1.首先,切换到你的项目空间,我的在 D:\ProjectsSpace\ElectronProjects\ElectronTest,ElectronTest是案例项目文 ...

  2. Android 开发之错误整理 [2014-04-28 09:22:28 - XXXX] Unable to resolve target 'android-18'

    在开发的时候难免会导入项目,那么怎么经常会遇到这个错误: [2014-04-28 09:22:28 - XXXX] Unable to resolve target 'android-18' targ ...

  3. ajax 第四步

    Ajax和XMLHttpRequest详述 (2011-12-10 16:40:23) 转载▼ 标签: ajax xmlhttprequest 分类: Web Ajax:Asynchronous Ja ...

  4. Android 瘦身之道 ---- so文件

    Android 瘦身之道 ---- so文件 [TOC] 1. 前言 目前Android 瘦身只有几个方面可以入手,因为apk的结构就已经固定了. res 目录下的资源文件.(通常是压缩图片,比如 矢 ...

  5. js的apply()与call()的区别

    1.各自对应的不同的语法: /*apply()方法*/ function.apply(thisObj[, argArray]) /*call()方法*/ function.call(thisObj[, ...

  6. Twitter数据抓取的方法(二)

    Scraping Tweets Directly from Twitters Search Page – Part 2 Published January 11, 2015 In the previo ...

  7. 线段树 poj 3468

    Description You have N integers, A1, A2, ... ,AN. You need to deal with two kinds of operations. One ...

  8. CTR预估中的贝叶斯平滑方法(二)参数估计和代码实现

    1. 前言 前面博客介绍了CTR预估中的贝叶斯平滑方法的原理http://www.cnblogs.com/bentuwuying/p/6389222.html. 这篇博客主要是介绍如何对贝叶斯平滑的参 ...

  9. mac双系统用磁盘工具合并windows分区后,开机还会 出现win分区

    如何删除开机硬盘的选择项 打开终端,输入sudo mount -t msdos /dev/disk0s1 /mnt 在Finer中会出现EFI盘,删除其中的Apple文件以外的文件即可(Apple千万 ...

  10. CSAcademy Beta Round #4 Swap Pairing

    题目链接:https://csacademy.com/contest/arhiva/#task/swap_pairing/ 大意是给2*n个包含n种数字,每种数字出现恰好2次的数列,每一步操作可以交换 ...