Linux Batch Find and Replace

 

Linux bulk find and replace folder contents of all files

 

Often used to find and replace batch of Linux, presented here using sed and grep command in conjunction with command for everyone to realize the search and replace the contents of the file.

 

Syntax:

sed -i "s / original string / new string / g" `grep -rl directory original string`

  

Example:

1. Find all files in batches contain baidu string in the current directory, for example:

grep -rn "baidu" ./

  

2. To achieve the bulk find and replace all files that contain the string baidu.com the current folder, and replace it with qq.com.

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

  

3. achieve mass find and replace all files that contain the string baidu.com / data folder, and replace it with qq.com.

sed -i "s/baidu.com/qq.com/g" `grep -rl "baidu.com" /data/`


Note that there is a space between the command and the parameters using the command!

 

4. A more complex example, as batch replace the URL https://www.baidu.com https://www.qq.com/hb

sed -i "s/https:\/\/www.baidu.com/https:\/\/www.qq.com\/hb/g" `grep -rl "https:\/\/www.baidu.com" ./`

  

 

Guess you like

Origin www.cnblogs.com/morgan363/p/11007389.html