我有一个低质量的PHP项目的问题(我没有写它:))和向Linux文件系统的迁移,也就是说,与Windows的区分大小写相反。包含和图像名称与我需要一种自动解决方案的真实文件名有些不一致。
所以我想到了一个脚本,它以递归方式解析项目目录中的php,js,jpg和gif文件,并用项目的php文件中的真实文件名替换错误案例文件名的所有出现。
算法就像:
在dir中找到$ foreach(php,js,jpg,png,gif ..):在dir中找到所有php:用$ found替换ignoreCase($ found)我试着用perl和File :: Find写这个,但我目前卡住了:/
答案 0 :(得分:1)
您可以使用find
和gnu-sed gsed
通过命令行执行此操作。
#!/bin/sh
# Replace filenames found in code and create a backup of original
find . -type f \( -name "*.php" -or -name "*.jpg" \) -exec bash -c 'gsed -i.bak "s/\(\W\)`basename {}`\(\W\)/\1`basename {}`\2/Ig" /src/ *' \;
########################
# Explanation
# find
# . : Recursively from current directory,
# -type f : files only - not folders,
# f\(..\) : with extensions,
# -ecec : execute this command,
# base -c : and execute in bash after {} replacement
# sed
# -i : Replace text in the actual file,
# .bak : but backup file to *.bak,
# s/ : search for;
# \W : something beginning with a
# non word so we dont match test.img with
# otherTest.img, but will still match "test.img"
# `basename {}` : file name placeholder provided by find
# \W : does not end in a word so we dont match
# test.htm against test.html
# /`basename {}`/ : Replace string from the search with the
# string of the actual filename
# g : Global search, match more than just the first instance
# I : Case insensitive search
# /src/* : Full path to files sed will work on