在多个目录内的所有文件中查找并替换 1 个单词?

在多个目录内的所有文件中查找并替换 1 个单词?

我的 WordPress 网站有问题,该网站使用 themefie.com 制作的外部主题,该网站受到 google 的限制,并且 google 也限制了我的网站,因为我正在使用那里的主题。

我在我的网站 public_html 目录中搜索了 themefie.com ,我发现很多文件包含 themefie.com ,我相信我应该从我的 WordPress 文件中删除这个域,这样谷歌就会删除对我网站的限制。

但问题是我发现超过 500 个文件中有 themefie.com ,我如何使用 SSH root 命令查找并用其他内容替换 themefie.com ?

Grep 示例输出:

zes":[]},"title_typography_font_weight":"400","_margin":{"unit":"px","top":"0","right":"0","bottom":"20","left":"0","isLinked":false},"text_align_mobile":"left"},"elements":[],"widgetType":"image-box"},{"id":"72847bd","elType":"widget","settings":{"image":{"url":"https:\/\/themefie.com\/wp\/foodka\/wp-content\/uploads\/2021\/08\/envelope.png","id":452,"alt":"","source":"library"},"title_text":"[email protected]","description_text":"","position":"left","image_space":{"unit":"px","size":10,"sizes":[]},"image_size":{"unit":"%","size":5,"sizes":[]},"content_v
<guid isPermaLink="false">https://themefie.com/wp/foodka/?p=495</guid>

答案1

您要求使用一个命令来删除多个文件中所有出现的字符串,因此它是:

find /home/xxx/public_html -type f -exec sed -i 's/themefie\.com//g' {} \;

如果你想用其他东西替换这个字符串:

find /home/xxx/public_html -type f -exec sed -i 's/themefie\.com/SOMETHING_ELSE/g' {} \;

您可以像这样备份文件(将为.bak所有修改的文件添加扩展名):

find /home/xxx/public_html -type f -exec sed -i.bak 's/themefie\.com/SOMETHING_ELSE/g' {} \;

然而,这是一种非常残酷的方法,而且发出这个命令很可能会破坏很多东西

如果您可以将主题更改为其他内容,那么您可能应该这样做。

无论发生什么,不要忘记备份您的数据在运行任何这些命令之前。

正如 @Stephen Kitt 在评论中所建议的,您可以find以不同的方式使用,这在使用sed.引用手册find页:

-exec command ;
              Execute command; true if 0 status is returned.  All following arguments to find are taken to be arguments to the command until an argument
              consisting of `;' is encountered.  The string `{}' is replaced by the current file name being processed everywhere it occurs in the  argu‐
              ments  to the command, not just in arguments where it is alone, as in some versions of find.  Both of these constructions might need to be
              escaped (with a `\') or quoted to protect them from expansion by the shell.  See the EXAMPLES section for examples of the use of the -exec
              option.   The specified command is run once for each matched file.  The command is executed in the starting directory.  There are unavoid‐
              able security problems surrounding use of the -exec action; you should use the -execdir option instead.

-exec command {} +
              This variant of the -exec action runs the specified command on the selected files, but the command line is built  by  appending  each  se‐
              lected  file name at the end; the total number of invocations of the command will be much less than the number of matched files.  The com‐
              mand line is built in much the same way that xargs builds its command lines.  Only one instance of `{}' is allowed within the command, and
              it  must appear at the end, immediately before the `+'; it needs to be escaped (with a `\') or quoted to protect it from interpretation by
              the shell.  The command is executed in the starting directory.  If any invocation with the `+' form returns a non-zero value as exit  sta‐
              tus,  then  find returns a non-zero exit status.  If find encounters an error, this can sometimes cause an immediate exit, so some pending
              commands may not be run at all.  For this reason -exec my-command ... {} + -quit may not result in my-command actually  being  run.   This
              variant of -exec always returns true.

所以这会给你:

find /home/xxx/public_html -type f -exec sed -i 's/themefie\.com/SOMETHING_ELSE/g' {} \+

相关内容