We often need to use batch find and replace in Linux. Here we introduce the use of thesedcommand to find content in files and replace it.

Syntax Format

sed -i "s/原字符串/新字符串/g" `grep 原字符串 -rl 所在目录`

Examples

In the following, we search the current directory for strings containing "baidu" and replace the string "baidu" with "example". Execute the command:

sed -i "s/baidu/example/g" `grep "baidu" -rl ./`

Next, let's use a more complex example to batch replace the URL libs.baidu.com with cdn.static.example.com:

sed -i "s/https:\/\/libs.baidu.com/https:\/\/cdn.static.example.com\/libs/g" `grep -rl "libs.baidu.com" ./`

Reference Commands