我需要替换一个文件夹中的许多文件中的字符串,只有ssh访问服务器。我该怎么做呢?


当前回答

如果你有可以使用的文件列表

replace "old_string" "new_string" -- file_name1 file_name2 file_name3

如果你有所有可以使用的文件

replace "old_string" "new_string" -- *

如果你有文件扩展名列表,你可以使用

replace "old_string" "new_string" -- *.extension

其他回答

grep --include={*.php,*.html} -rnl './' -e "old" | xargs -i@ sed -i 's/old/new/g' @

使用ack命令会快得多,像这样:

ack '25 Essex' -l | xargs sed -i 's/The\ fox \jump/abc 321/g'

如果你在搜索结果中有空白。你需要逃离它。

我使用ag, the_silver_searcher:

ag -0 -l 'old' | xargs -0 sed -ri.bak -e 's/old/new/g';

然后git清除。bak文件(rm在git rebase exec中运行时出现bug)

git clean -f '**/*.bak';

如果字符串中有正斜杠(/),则可以将分隔符更改为'+'。

find . -type f -exec sed -i 's+http://example.com+https://example.com+g' {} +

该命令将在当前目录中递归地运行。

要替换文件中的路径(避免转义字符),您可以使用以下命令:

sed -i 's@old_path@new_path@g'

@符号意味着在接下来的字符串中所有的特殊字符都应该被忽略。