How to replace a string in multiple files in linux command line
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Linux, replacing a string in multiple files is a common task, especially for developers and system administrators. The command line, with its powerful tools and utilities, allows users to perform this task efficiently. Here, we will explore how to use tools like sed, awk, grep, and find for string replacement across multiple files.
Using sed for String Replacement
The stream editor (sed) is a powerful utility for performing text transformations on an input stream (a file or input from a pipeline). It is widely used for substituting text patterns. Here’s how you can use sed to replace strings:
Basic Syntax
The basic syntax for replacing text with sed is:
-i: Edit files in-place (otherwise,sedoutputs to standard output).s/original/replacement/: This is the substitution command. Thesstands for substitute.originalis the text to be replaced, andreplacementis the new text.g: Apply the replacement globally in each line.
Example: Replacing Text in Multiple Files
If you want to replace "hello" with "world" in all .txt files in the current directory, you can use:
Using grep with sed
While sed is powerful, combining it with grep can efficiently target files that contain a specific string, reducing unnecessary processing. Here’s an example using grep and sed together:
grep -rl 'hello' ./finds all files in the current directory and subdirectories recursively that contain "hello".xargs sed -i 's/hello/world/g'takes the list of files and passes them tosedto perform the replacement.
Using awk
Although less common for simple substitutions, awk is a full-fledged programming language that shines in text processing for more complex scenarios:
gsub(/hello/, "world"): Globally replace "hello" with "world".- Output is redirected to a temporary file which replaces the original.
Automation with find
For a more comprehensive solution, particularly when dealing with a large number of files or when files are spread across numerous directories, use find:
find . -type f -name '*.txt': Find all.txtfiles in the current directory and its subdirectories.-exec: Execute the following command on each file found.{}: Placeholder for the current file name.+: Indicates the end of the command.
Summary Table
| Tool | Command Example | Description | |
sed | sed -i 's/hello/world/g' *.txt | In-place replacement within files directly specified. | |
grep + sed | `grep -rl 'hello' ./ \ | xargs sed -i 's/hello/world/g'` | Efficiently target and replace in files that contain the string. |
awk | awk '{gsub(/hello/, "world"); print}' file.txt > temp_file | Useful for more complex text processing tasks. | |
find + sed | find . -type f -name '*.txt' -exec sed -i 's/hello/world/g' {} + | Comprehensive search and replace across directories. |
Conclusion
Mastering string replacement on the Linux command line can greatly enhance your productivity and ability to manage large volumes of text data. The choice of tool—whether sed, awk, or a combination with grep and find—depends on the specific requirements of the task and the complexity of the operations involved. By utilizing these commands, you can automate the editing process across multiple files efficiently.

