我需要替换一个文件夹中的许多文件中的字符串,只有ssh访问服务器。我该怎么做呢?
当前回答
要替换文件中的路径(避免转义字符),您可以使用以下命令:
sed -i 's@old_path@new_path@g'
@符号意味着在接下来的字符串中所有的特殊字符都应该被忽略。
其他回答
cd /path/to/your/folder
sed -i 's/foo/bar/g' *
出现的“foo”将被替换为“bar”。
在像macOS这样的BSD系统上,你需要提供一个备份扩展名,比如-i '.bak',否则每个manpage都会“冒损坏或部分内容的风险”。
cd /path/to/your/folder
sed -i '.bak' 's/foo/bar/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';
类似于Kaspar的答案,但是用g标记来替换一行上的所有出现。
find ./ -type f -exec sed -i 's/old_string/new_string/g' {} \;
对于全局不区分大小写:
find ./ -type f -exec sed -i 's/old_string/new_string/gI' {} \;
如果你有可以使用的文件列表
replace "old_string" "new_string" -- file_name1 file_name2 file_name3
如果你有所有可以使用的文件
replace "old_string" "new_string" -- *
如果你有文件扩展名列表,你可以使用
replace "old_string" "new_string" -- *.extension
要替换文件中的路径(避免转义字符),您可以使用以下命令:
sed -i 's@old_path@new_path@g'
@符号意味着在接下来的字符串中所有的特殊字符都应该被忽略。