Best way to do a find/replace in several files?

后端 未结 5 547
北恋
北恋 2021-01-30 01:56

what\'s the best way to do this? I\'m no command line warrior, but I was thinking there\'s possibly a way of using grep and cat.

I just want to

5条回答
  •  心在旅途
    2021-01-30 02:29

    find . -type f -print0 | xargs -0 -n 1 sed -i -e 's/from/to/g'
    

    The first part of that is a find command to find the files you want to change. You may need to modify that appropriately. The xargs command takes every file the find found and applies the sed command to it. The sed command takes every instance of from and replaces it with to. That's a standard regular expression, so modify it as you need.

    If you are using svn beware. Your .svn-directories will be search and replaced as well. You have to exclude those, e.g., like this:

    find . ! -regex ".*[/]\.svn[/]?.*" -type f -print0 | xargs -0 -n 1 sed -i -e 's/from/to/g'
    

    or

    find . -name .svn -prune -o -type f -print0 | xargs -0 -n 1 sed -i -e 's/from/to/g'
    

提交回复
热议问题