Migrating a legacy PHP Project from Windows to Linux (inconsistent case disaster)

521

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 :/

388

Answer

Solution:

You can do this with a command line usingfind and gnu-sedgsed.

#!/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

People are also looking for solutions to the problem: php - Preg matching Arabic?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.