複数のディレクトリ内のすべてのファイルで1単語を見つけて置き換えますか?

複数のディレクトリ内のすべてのファイルで1単語を見つけて置き換えますか?

themefie.comで作成した外部テーマを使用するWordPressサイトに問題があり、サイトがGoogleによって制限され、そこでテーマを使用しているため、Googleでもサイトを制限しました。

私のウェブサイトのpublic_htmlディレクトリでthemefie.comを検索し、themefie.comを含む多くのファイルを見つけました。 Googleが自分のウェブサイトの制限を削除できるように、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

複数のファイルから文字列内のすべてのエントリを削除するために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' {} \+

おすすめ記事