Welcome 微信登录

首页 / 操作系统 / Linux / shell实现文件名相同路径不同的批量复制

如果系统存在文件名相同,但路径不同的文件,如果单纯用find来批量复制到一个地方的话会被覆盖掉,下面的脚本是实现根据文件名的路径来进行存放复制。为能更好的测试,脚本中加了在不同路径创建相同文件名的程序。
  1. #!/bin/sh
  2. . /etc/profile
  3. # define
  4. tf=testfile
  5. destpath=/root/found
  6. [ ! -d $destpath ] && mkdir -p $destpath
  7. # touch some the same file for test
  8. TouchFile()
  9. echo "/tmp" > /tmp/$tf
  10. echo "/home" > /home/$tf
  11. echo "/root" > /root/$tf
  12. echo "/var/tmp" > /var/tmp/$tf
  13. }
  14. # find the file and copy to the dest dir
  15. FindCopy()
  16. {
  17. TouchFile
  18. if [ $? -eq 0 ];then
  19.     for i in $(find / -name $tf);do
  20.         [ ! -d $destpath/$(dirname $i) ] && mkdir -p $destpath$(dirname $i)
  21.         cp -rf $i $destpath$(dirname $i) 
  22.         #echo $i
  23.     done
  24. else
  25.     echo "please touch some test file first..."
  26. fi
  27. }
  28. FindCopy
Gentoo Chrome浏览器在Linux root 权限下启动发生 user-data-dir修正Linux系统命令su和su - 区别验证相关资讯      shell  Shell脚本 
  • 在shell脚本中使用函数  (今 11:53)
  • 使用shell批量生成数据整合式迁移  (03月29日)
  • shell脚本中报错dirname:无效选项  (12/21/2015 19:12:37)
  • Shell脚本 入门 —— 符号篇  (08月16日)
  • Linux入门学习:认识shell和bash  (03月25日)
  • shell脚本:监控MySQL服务是否正常  (11/19/2015 20:05:43)
本文评论 查看全部评论 (0)
表情: 姓名: 字数


评论声明
    版权所有©石家庄振强科技有限公司2024 冀ICP备08103738号-5 网站地图