shell实现文件名相同路径不同的批量复制

字体大小: 中小 标准 ->行高大小: 标准

如果系统存在文件名相同,但路径不同的文件,如果单纯用find来批量复制到一个地方的话会被覆盖掉,下面的脚本是实现根据文件名的路径来进行存放复制。为能更好的测试,脚本中加了在不同路径创建相同文件名的程序。

  1. #!/bin/sh
  2. . /etc/profile
  3. # define tf=testfile
  4. destpath=/root/found [ ! -d $destpath ] && mkdir -p $destpath # touch some the same file for test
  5. TouchFile() { 
  6. echo "/tmp" > /tmp/$tf echo "/home" > /home/$tf
  7. echo "/root" > /root/$tf echo "/var/tmp" > /var/tmp/$tf
  8. } # find the file and copy to the dest dir
  9. FindCopy() {
  10. TouchFile if [ $? -eq 0 ];then
  11.     for i in $(find / -name $tf);do         [ ! -d $destpath/$(dirname $i) ] && mkdir -p $destpath$(dirname $i)
  12.         cp -rf $i $destpath$(dirname $i)          #echo $i
  13.     done else
  14.     echo "please touch some test file first..." fi
  15. } FindCopy

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/64902.html