How to replace text in multiple files using Linux command line

Published on Aug. 22, 2023, 12:20 p.m.

To replace text in multiple files using Linux command line, you can use the sed command with -i option. Here’s an example command:

sed -i 's/string_to_replace/replacement_string/g' file1.txt file2.txt file3.txt

In the above command, replace string_to_replace with the text that you want to replace and replacement_string with the text that you want to replace it with. The g at the end of the command stands for global, which means that all occurrences of the string will be replaced, not just the first one. Replace file1.txt file2.txt file3.txt with the actual filenames that you want to modify.

You can also use wildcards to perform the same action on multiple files at once. For example:

sed -i 's/string_to_replace/replacement_string/g' *.txt

This will replace all instances of string_to_replace with replacement_string in all files with a .txt extension in the current directory.

I hope that helps! Let me know if you have further questions.

Tags: