Skip to content

Change multiple files with sed by using xargs⚓︎

Sed doesn’t natively support for editing multiple files. One way to accomplish an edit of multiple files is to grep for the string, pass it through xargs, and then to sed with in-place edit.

grep -rl 'original_text' . | xargs -0 --no-run-if-empty sed -i 's|original_text|new_text|g'
  • grep recursively, and list file names containing the match
  • xargs splits only on a null characters, not on whitespace in file names
  • sed then edits each file, saving the changes back to the original file