How to replace a string in a file in Linux

subject: Commands · date: 2017-12-09

To replace string foo with bar in files:

  • Non recursive, files in current directory only:

    $ sed -i -- 's/foo/bar/g' *
    
  • Recursive, regular files (including hidden ones) in this and all subdirectories:

    $ find . -type f -exec sed -i 's/foo/bar/g' {} +
    

read more

share this article