replace_file.sh 904 B

123456789101112131415161718192021222324252627282930313233
  1. #!/bin/bash
  2. #### 批量替换wp项目代码中的文件
  3. #### Author: chenpeiqing
  4. #### Date: 2025-01-07
  5. # 指定源文件和目标目录
  6. source_file="/opt/adweb3/config/wp-html.php"
  7. target_directory="/usr/local/wwwtemplate/"
  8. # 检查源文件是否存在
  9. if [ ! -f "$source_file" ]; then
  10. echo "源文件不存在"
  11. exit 1
  12. fi
  13. # 检查目标目录是否存在
  14. if [ ! -d "$target_directory" ]; then
  15. echo "目标目录不存在"
  16. exit 1
  17. fi
  18. # 获取目标目录的第一级子目录
  19. first_level_subdir=$(find "$target_directory" -maxdepth 1 -type d -printf "%P\n")
  20. echo "获取到所有子目录:$first_level_subdir"
  21. # 如果目标目录下存在子目录,则复制文件到第一级子目录中
  22. # 循环遍历子目录并复制文件
  23. for subdir in $first_level_subdir; do
  24. cp -f $source_file $target_directory$subdir
  25. echo "Copied $source_file to $target_directory$subdir"
  26. done