Replace a string in multiple files in Linux

We often come across a requirement to replace a string in a set of files. If we are using a windows environment, then doing it would be easy. Linux also provides some commands to search for a string in a file and replace all of them at once. We will look at multiple options that linux operating system provides to replace a string in multiple files at once in Linux.

Table of Contents

Find and replace a string in files with grep and sed commands

grep is one of the most widely used command in linux operating system to search for a string in multiple files. Once we identify all the files that has the matching pattern, then we can use sed command to replace the string.

Syntax:

grep -rl <search_string> <directory_path> | xargs sed -i 's/<search_string>/<replace_string>/g'

Parameters:

Advertisements
  • r : searches all the sub-directories recursively
  • l : print only names of FILEs with selected lines
  • i : by default sed writes output to standard output. Using this option will will tell to edit the file directly
  • g : By default sed only replaces the first occurrence, so using this we will tell sed to replace all occurrences globally

Examples:

If we want to find a specified string in all files in a directory and replace all occurrences with another string in those files, then use the below command,

grep -rl 'head' www/ | xargs sed -i 's/head/header/g'

The above command will try to find all files in “www” folder that has a string “head” and then replace all those occurrences with string “header”.

Sometimes we need to exclude certain directories from the search. In those cases we can use the below syntax

Syntax:

grep -rl --exclude-dir=<dir> <search_string> <directory_path> | xargs sed -i 's/<search_string>/<replace_string>/g'

Examples:

grep -rl --exclude-dir='script' 'head' www/ | xargs sed -i 's/head/header/g'

Find and replace a string in files with find and sed command

We can also use find command to search for a string in multiple files, then we can use sed command to replace the string.

Syntax:

find . -name <regular_expression> -type f -print0 | xargs -0 sed -i 's/<search_string>/<replace_string>/g'
  • f : searches only files
  • i : by default sed writes output to standard output. Using this option will will tell to edit the file directly
  • g : By default sed only replaces the first occurrence, so using this we will tell sed to replace all occurrences globally

Examples:

If we want to find a string in all html files with-in a directory and replace all occurrences with a string then use the below command

find . -name '*.html' -type f -print0 | xargs -0 sed -i 's/head/header/g'

The above command will try to find all html files in current directory and replace the string “head” with “header” in all files under this directory.

Sometimes we need to exclude certain directories from the search. In those cases we can use the below syntax

Syntax:

find . -type f -not -path <regular_expression> -print0 | xargs -0 sed -i 's/<search_string>/<replace_string>/g'

Examples:

find . -type f -not -path '*/\.*' -print0 | xargs -0 sed -i 's/head/header/g'

The above command will try to find all html files in current directory except the hidden files and replace the string “head” with “header” in all files under this directory.

Find and replace a string in files with find and exec commands

This option is similar to above operation. The only difference is instead of using a Pipe operator to pass output of one command to another, we use exec command and execute the sed command on the results returned by find operation. Let’s look at its syntax below

Syntax:

find . -name <regular_expression> -type f -exec sed -i 's/<search_string>/<replace_string>/g' {} \+;

Parameters

  • f : searches only files
  • i : by default sed writes output to standard output. Using this option will will tell to edit the file directly
  • g : By default sed only replaces the first occurrence, so using this we will tell sed to replace all occurrences globally
  • {} : placeholder that holds the files returned by the find command
  • +; : execute the command for all files found by find at once like cmd result1 result2. This should only be used if the command accepts multiple parameters at once. If the command only accepts one argument at a time, then use \; instead of +;

Examples:

find . -name '*.html' -type f -exec sed -i 's/head/header/g' {} \+;

The above command will try to find all html files in current directory and replace the string “head” with “header” in all files under this directory.

Sometimes we need to exclude certain directories from the search. In those cases we can use the below syntax,

Syntax:

find . -type f -not -path <regular_expression> -exec sed -i 's/<search_string>/<replace_string>/g' {} +

Examples:

find . -type f -not -path '*/\.*' -exec sed -i 's/head/header/g' {} +

The above command will try to find all html files in current directory except the hidden files and replace the string “head” with “header” in all files under this directory.

Summary:

In this guide, we have learnt multiple ways in which we can replace a string in multiple files at once in Linux operating system. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top