How to Replace a String in Multiple Files in Linux Command Line

How to Replace a String in Multiple Files in Linux Command Line

I need to replace a string in a lot of files in a folder, with only ssh access to the server. How can I do this?

1

27 Answers

cd /path/to/your/folder
sed -i 's/foo/bar/g' *

Occurrences of "foo" will be replaced with "bar".

On BSD systems like macOS, you need to provide a backup extension like -i '.bak' or else "risk corruption or partial content" per the manpage.

cd /path/to/your/folder
sed -i '.bak' 's/foo/bar/g' *
14

Similar to Kaspar's answer but with the g flag to replace all the occurrences on a line.

find ./ -type f -exec sed -i 's/string1/string2/g' {} \;

For global case insensitive:

find ./ -type f -exec sed -i 's/string1/string2/gI' {} \;
15

@kev's answer is good, but only affects files in the immediate directory.The example below uses grep to recursively find files. It works for me everytime.

grep -rli 'old-word' * | xargs -i@ sed -i 's/old-word/new-word/g' @

Command breakdown

grep -r: --recursive, recursively read all files under each directory.
grep -l: --print-with-matches, prints the name of each file that has a match, instead of printing matching lines.
grep -i: --ignore-case.

xargs: transform the STDIN to arguments, follow this answer.
xargs -i@ ~command contains @~: a placeholder for the argument to be used in a specific position in the ~command~, the @ sign is a placeholder which could replaced by any string.

sed -i: edit files in place, without backups.
sed s/regexp/replacement/: substitute string matching regexp with replacement.
sed s/regexp/replacement/g: global, make the substitution for each match instead of only the first match.

11

There are a few standard answers to this already listed. Generally, you can use find to recursively list the files and then do the operations with sed or perl.

rpl

For most quick uses, you may find the command rpl is much easier to remember.

Replace foo with bar on all .txt files:

rpl -v foo bar '*.txt' 

Simulate replacing the regex foo.* with bar in all .txt files recursively:

rpl --dry-run 'foo.*' bar '**/*.txt'

You'll probably need to install it (apt-get install rpl or similar).

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.