[Switch] find+xargs+sed batch replacement

 

When writing code, it is often encountered that some contents in the .c and .h files need to be replaced. Using sourceinsight for global search is a method, but sourceinsight can only replace strings in one file, not at the same time. The same character in multiple files, use find under linux, combined with sed, xargs can achieve this function.

Replace all "waf" characters in all .c and .h files in the current directory with "tamper"

find . -name "*.[ch]" |xargs sed -i 's/waf/tamper/g'

 

Replace all files containing "linux" characters in the /opt/test/ directory with windows

The command is as follows
find /opt/test/ -type f | awk -F":" '{print $1}' |xargs sed -i's/linux/windows/g'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325028600&siteId=291194637