A
A
anriko2021-03-15 10:48:58
Command line
anriko, 2021-03-15 10:48:58

How to replace a phrase in a file in different folders?

phrase $GLOBALS["SLIDER_TOP"] = 17258; to $GLOBALS["PREIM_TOP"] = 3670; ?
on all subdomains there is a folder stolbiki-parkovochnye in it index.php how to make a replacement in all folders of the index.php file?
like this find
find ./*/public_html/stolbiki-parkovochnye -name "index.php" | But how can I make a replacement in the file?
or is it correct?
grep -irl '$GLOBALS["SLIDER_TOP"] = 17258;' ./ekb/public_html | xargs -IX sed -i 's|$GLOBALS["SLIDER_TOP"] = 17258;|$GLOBALS["PREIM_TOP"] = 3670; |' X

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Taran, 2021-03-15
@anriko

Read to the end at the beginning

find  ./*/public_html/stolbiki-parkovochnye -type f -name 'index.php' -exec sed -i 's/\x24GLOBALS\[\x22SLIDER_TOP\x22\] = 17258\x3B/GLOBALS["PREIM_TOP"] = 3670;/g' {} \;

./*/public_html/stolbiki-parkovochnye - from which folder the
-type f file
-name 'index.php'is - only the php index
-exec- execute with the found one
sed -i 's/было/стало/g'- zaenit in the stream and overwrite all found matches in the file
'\x24 и \x22 и \x3B I wrote this here
{} - the miracle you were looking for is the argument that you found the find it is substituted (part of exec)
\;- close the exec
result
cat index.php
GLOBALS["PREIM_TOP"] = 3670;

If you want to look at the result but not make changes, instead sed -ichange to sed -e
Since sed is dangerous for inexperienced use, it would not be sour to make a backup of these files while maintaining the directory structure.
mkdir ./tmp/backup
find./*/public_html/stolbiki-parkovochnye -type f -name 'index.php' -print0 | xargs -0 cp --parents --target-directory ./tmp/backup

thanks cap, a very detailed answer, I didn’t even expect it right -> "the issue is resolved"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question