How to grep and replace

后端 未结 9 1596
囚心锁ツ
囚心锁ツ 2020-11-27 08:47

I need to recursively search for a specified string within all files and subdirectories within a directory and replace this string with another string.

I know that t

9条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 09:36

    Here is what I would do:

    find /path/to/dir -type f -iname "*filename*" -print0 | xargs -0 sed -i '/searchstring/s/old/new/g'
    

    this will look for all files containing filename in the file's name under the /path/to/dir, than for every file found, search for the line with searchstring and replace old with new.

    Though if you want to omit looking for a specific file with a filename string in the file's name, than simply do:

    find /path/to/dir -type f -print0 | xargs -0 sed -i '/searchstring/s/old/new/g'
    

    This will do the same thing above, but to all files found under /path/to/dir.

提交回复
热议问题