Skip to content

Instantly share code, notes, and snippets.

@brendanfalkowski
Last active August 10, 2022 17:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brendanfalkowski/7274294 to your computer and use it in GitHub Desktop.
Save brendanfalkowski/7274294 to your computer and use it in GitHub Desktop.
Replace all occurrences of one string with another string in all child directories matching specific file types. Useful for normalizing Magento's copyright changes to get code-only diffs between releases.
############################
# Basic usage
############################
cd path/to/project
# One file type:
# Replace "red rover" with "black dog" in PHP files
find . -name '*.php' -print0 | xargs -0 sed -i '' 's/red rover/black dog/g'
# Multiple file types:
# Replace "red rover" with "black dog" in PHP or XML or PHTML files
find . -name '*.php' -print0 -o -name '*.phtml' -print0 -o -name '*.xml' -print0 | xargs -0 sed -i '' 's/red rover/black dog/g'
############################
# Magento usage
############################
# Optimized for scanning Magento.
# Using "*.ext*" to catch files named like "xyz.php.sample".
# Checks file types:
#
# *.css*
# *.html*
# *.js*
# *.php*
# *.phtml*
# *.xml*
find . -name '*.css*' -print0 -o -name '*.html*' -print0 -o -name '*.js*' -print0 -o -name '*.php*' -print0 -o -name '*.phtml*' -print0 -o -name '*.xml*' -print0 | xargs -0 sed -i '' 's/(c) 2013 Magento/(c) 2012 Magento/g'
# If there is error output:
# sed: RE error: illegal byte sequence
#
# Then add arguments to xargs to show which files have issues.
# Warning: this will leave dot-file copies of the files that sed can't process.
# Usually the cause is UTF-8 chars (like accented letters) being used in comments.
#
# See: https://gist.github.com/kalenjordan/6766591#comment-942244
find . -name '*.css*' -print0 -o -name '*.html*' -print0 -o -name '*.js*' -print0 -o -name '*.php*' -print0 -o -name '*.phtml*' -print0 -o -name '*.xml*' -print0 | xargs -t -n 1 -0 sed -i '' 's/(c) 2013 Magento/(c) 2012 Magento/g'
@drobinson
Copy link

The extra '' in sed -i '' 's/(c) 2013 Magento/(c) 2012 Magento/g' broke this for me - once I removed those quotes it worked. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment