Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Replacing strings in files based on certain search criteria is a very common task. How can I

  • replace string foo with bar in all files in the current directory?
  • do the same recursively for sub directories?
  • replace only if the file name matches another string?
  • replace only if the string is found in a certain context?
  • replace if the string is on a certain line number?
  • replace multiple strings with the same replacement
  • replace multiple strings with different replacements
share|improve this question
This is intended to be a canonical Q&A on this subject (see this meta discussion), please feel free to edit my answer below or add your own. –  terdon Feb 1 at 17:08
I just want to replace foo with bar in a specific line. all the lines contains foo's.what to do? –  user2433165 Sep 4 at 11:38
@user2433165 look at the last bullet point of solution number 3 in my answer below. Just change 4 to the line number you want. –  terdon Sep 4 at 14:05
Umm... is it wise to use nonportable tools/options in canonical Q/As? –  mikeserv Sep 4 at 17:30
@mikeserv I don't see why not. My answer is basically sed, awk and perl all of which are available on the vast majority of systems. Note that it's a community wiki answer and is so precisely to encourage people to edit and improve it. If you have more portable solutions, please add them. –  terdon Sep 4 at 18:23

1 Answer 1

up vote 36 down vote accepted

1. Replacing one string with another in all files in the current directory:

These are for cases where you know that the directory contains only files and that you want to process all files. If that is not the case, use the approaches in 2.

  • Non recursive, files in this directory only:

    sed -i 's/foo/bar/' *
    perl -i -pe 's/foo/bar/' * 
  • Recursive, files in this and all subdirectories

    find . -type f -exec sed -i 's/foo/bar/' {} +

    If you are using bash:

    shopt -s globstar


    sed -i 's/foo/bar/' **/*

2. Replace only if the file name matches another string / has a specific extension / is of a certain type etc:

  • Non-recursive, files in this directory only:

    sed -i 's/foo/bar/' *baz*    ## all files whose name contains baz
    sed -i 's/foo/bar/' *.baz    ## files ending in .baz
  • Recursive, files in this and all subdirectories

    find . -type f -name "*baz*" -exec sed -i 's/foo/bar/' {} +

    If you are using bash:

    shopt -s globstar


    sed -i 's/foo/bar/' **/*baz*
    sed -i 's/foo/bar/' **/*.baz

    Note that these will fail if you have directories whose name contains baz. It is safer to use find to target only files.

  • If a file is of a certain type, for example, executable (see man find for more options):

    find . -type f -executable -exec sed -i 's/foo/bar/' {} +

3. Replace only if the string is found in a certain context

  • Replace foo with bar only there is a baz later on the same line:

    sed -i 's/foo\(.*baz\)/bar\1/' file

    In sed, using \( \) saves whatever is in the parentheses and you can then access it with \1. There are many variations of this theme, to learn more about such regular expressions, see here.

  • Replace foo with bar only if foo is found on the 3d column (field) of the input file (assuming whitespace-separated fields):

    gawk 'gsub(/foo/,"baz",$3)' file > newfile && mv newfile file

    For a different field just use $N where N is the number of the field of interest. For a different field separator (: in this example) use:

    gawk -F':' 'gsub(/foo/,"baz",$3)' file > newfile && mv newfile file

    Another solution that does in-place editing:

    perl -i -ane '$F[2]=~s/foo/baz/; print "@F\n"' foo 

    NOTE: The perl solution will print space separated fields even if the input file had tabs. For a different field use $F[N-1] where N is the field umber you want and for a differet field separator use (the $"=":" sets the output field separator to :):

    perl -i -F':' -ane '$F[2]=~s/foo/baz/; $"=":";print "@F"' foo 
  • Replace foo with bar only on the 4th line:

    gawk '(NR==4){gsub(/foo/,"baz")};1;' foo > newfile && mv newfile file
    perl -i -pe 's/foo/bar/g if $.==4' foo

4. Multiple replace operations: replace with different strings

You can combine sed commands:

sed -i 's/foo/bar/; s/baz/zab; s/Alice/Joan/' file

or Perl commands

perl -i -pe 's/foo/bar/; s/baz/zab; s/Alice/Joan/' file

If you have a large number of patterns, it is easier to save your patterns and their replacements in a file and read it with a little script. For example, create a file called patterns.txt with the patterns and replacements, one per line:

foo bar
baz zab

Then, read the file and do your replacements (note the double quotes in the sed command):

while read pattern replacement; do   
 sed -i "s/$pattern/$replacement/" file
done < patterns.txt

5. Multiple replace operations: replace multiple patterns with the same string

Replace any of foo, bar or baz with foobar

sed -i 's/foo\|bar\|baz/foobar/' file


perl -i -pe 's/foo|bar|baz/foobar/' file
share|improve this answer
A version with ed should be added because sed -i is not supported on lots of sed version. but I never manage to use ed correctly –  Kiwy Feb 7 at 12:52
@kiwy does ed have an -i option? And which sed implementations don't? I know that both GNU and BSD sed do. Also, perl will always have it. Anyway, feel free to add/edit as you wish, that's why I made this community wiki. –  terdon Feb 7 at 13:06
that's the nice thing with AIX, when everybody except something simple and heavily spread will work, it will not on this system. -.- anyway, I will add the case where sed does not enable inline edition. –  Kiwy Feb 7 at 13:08
this post is amazing. kudos. –  son_of_fire Feb 11 at 10:07
@somethingSomething, the sed command does not rename! It reads every line of every input file and replaces the first occurrence of foo with bar on each line. Of course that will take ages for 13GB of files! –  terdon Nov 2 at 14:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.