Linux
Command Line
String Replacement
Programming
File Management

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:

bash
sed -i 's/original/replacement/g' file
  • -i: Edit files in-place (otherwise, sed outputs to standard output).
  • s/original/replacement/: This is the substitution command. The s stands for substitute. original is the text to be replaced, and replacement is 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:

bash
sed -i 's/hello/world/g' *.txt

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:

bash
grep -rl 'hello' ./ | xargs sed -i 's/hello/world/g'
  • 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 to sed to 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:

bash
awk '{gsub(/hello/, "world"); print}' file.txt > temp_file && mv temp_file file.txt
  • 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:

bash
find . -type f -name '*.txt' -exec sed -i 's/hello/world/g' {} +
  • find . -type f -name '*.txt': Find all .txt files 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

ToolCommand ExampleDescription
sedsed -i 's/hello/world/g' *.txtIn-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.
awkawk '{gsub(/hello/, "world"); print}' file.txt > temp_fileUseful for more complex text processing tasks.
find + sedfind . -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.


Course illustration
Course illustration

All Rights Reserved.