1

i have a Problem with a low-quality PHP Project (i didnt write it :)) and a migration towards a linux file system, that is, in contrast to windows' case sensitive. The includes and image-names are somewhat inconsistent to the real filenames that i need a kind of automatic solution.

So i thought about a script, that parses the project directory recursively for for php, js, jpg and gif files and replaces all occurrencies of the wrong case filename with the real filename in the project's php files.

the algorithm would be like:

foreach (php,js,jpg,png,gif..) as $found in dir: find all php in dir : replace ignoreCase($found) with $found

i tried to write this with perl and File::Find, but im currently stuck :/

tadman
  • 208,517
  • 23
  • 234
  • 262
DudiDude
  • 355
  • 3
  • 12
  • 1
    Pragmatic approach: Make everything lowercase. Files and all references to files. – SBI Nov 11 '14 at 17:04
  • 1
    Please show your code so far, as it's much easier to give you a decent answer. – Sobrique Nov 11 '14 at 17:11
  • The problem here is you need to develop a mapping table of their case-sensitive names. Otherwise you might want to just rename them all lower-case and do the same in the source. – tadman Nov 11 '14 at 17:16
  • 1
    Add code to the question, and possible duplicate, http://stackoverflow.com/questions/1998156/case-insensitive-urls-with-mod-rewrite – mpapec Nov 11 '14 at 17:18

1 Answers1

1

You can do this with a command line using find and 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
harvey
  • 2,945
  • 9
  • 10
  • Hi harvey, thanks alot for your answer and the great documentation you wrote. The only problem seems to be, that the NON-Word \W also get replaced by the basename in your code... – DudiDude Nov 12 '14 at 09:44
  • I actually caught that earlier but didn't get to update the post. Now fixed with group matches. – harvey Nov 12 '14 at 12:27