How to replace a string in multiple files at once?
When there are files in one directory:
perl -pi -e 's/stary-string/nowy-string/g' *
If in one file then:
perl -pi -e 's/stary-string/nowy-string/g' db.sh
If the searched string contains characters that we used as a separator after the s operator, then we must use other separating characters for the s operator, i.e. those that do not appear in the searched string, e.g. { }:
find . -name '*.html' |xargs perl -pi -e 's{\Q<script src=..\..\E}{ }g'
Note: The text between \Q and \E is interpreted “literally”, without distinguishing the characters appearing there, then the dots will be treated as dots – and as we know, by default in a regular expression, a dot means any character. In the given example, we want to remove the expression “<script src=..\..” from the html files.
Or with the popular sed:
sed -e 's/zczego/naco/g' -i ./plik
find . -name '*.php' -type f -exec sed -i 's/zCzego/naCo/g' {} +