Search and Replace Multiple Files using Sed

Here is a quick command that you can use to search and replace a piece of text across multiple files using find, xargs, and sed.

find . -name "*.txt" -print | xargs sed -i "s/foo/bar/g"

Explanation

find . -name "*.txt" -print

This part of the command finds all of the files in the current directory that match the /*.txt/ pattern (in other words, all files that end with /.txt/.

xargs

This command is used to pass standard input to another command. It uses the single line output of the previous find command and executes the following sed command on each of the files.

sed -i “s/foo/bar/g”

This command does an /in place/ search and replace (meaning it overwrites the existing file). It uses the regular expression provided in quotes to do that replacement. In this case, replacing /foo/ with /bar/.

See Also

If you found this useful you might also like the following articles.

Using AWK on CSV Files

Using AWK and SED on CSV or TEXT Files

Technical Documents

comments powered by Disqus
linux/search_and_replace_multiple_files_using_sed.txt · Last modified: 2020/06/01 22:53 (external edit)