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

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