How to find and replace all occurrences of a string recursively in a directory tree?

前端 未结 8 1596
野性不改
野性不改 2020-12-12 18:01

Using just grep and sed, how do I replace all occurrences of:

a.example.com

with

b.example.com

within a

8条回答
  •  悲哀的现实
    2020-12-12 18:31

    Try this:

    grep -rl 'SearchString' ./ | xargs sed -i 's/REPLACESTRING/WITHTHIS/g'
    

    grep -rl will recursively search for the SEARCHSTRING in the directories ./ and will replace the strings using sed.

    Ex:

    Replacing a name TOM with JERRY using search string as SWATKATS in directory CARTOONNETWORK

    grep -rl 'SWATKATS' CARTOONNETWORK/ | xargs sed -i 's/TOM/JERRY/g'
    

    This will replace TOM with JERRY in all the files and subdirectories under CARTOONNETWORK wherever it finds the string SWATKATS.

提交回复
热议问题