歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> shell實現文件名相同路徑不同的批量復制

shell實現文件名相同路徑不同的批量復制

日期:2017/2/28 15:29:04   编辑: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. {
  10. echo "/tmp" > /tmp/$tf
  11. echo "/home" > /home/$tf
  12. echo "/root" > /root/$tf
  13. echo "/var/tmp" > /var/tmp/$tf
  14. }
  15. # find the file and copy to the dest dir
  16. FindCopy()
  17. {
  18. TouchFile
  19. if [ $? -eq 0 ];then
  20. for i in $(find / -name $tf);do
  21. [ ! -d $destpath/$(dirname $i) ] && mkdir -p $destpath$(dirname $i)
  22. cp -rf $i $destpath$(dirname $i)
  23. #echo $i
  24. done
  25. else
  26. echo "please touch some test file first..."
  27. fi
  28. }
  29. FindCopy
Copyright © Linux教程網 All Rights Reserved