Replace a string in multiple files

Replace a string in multiple files

Sometimes, you may need to replace a text in so many files in a folder recursively. In that situation below command will help you get resolved.

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

Let me explain above command :-

` Find command` :-

The Linux find command is a very useful and handy command to search for files from the command line. It can be used to find files based on various search criterias like permissions, user ownership, modification date/time, size etc.

So, in order to search in specific directory or path, we provide below command :-

find /path/to/folder

For an example :-

find /home/john/Desktop -name "w3clan.txt"

Above command will search for a file with name "w3clan.txt" in "/home/john/Desktop" , you could have also used wildcard to search for specific extension like :-

find /var/www/html/ -name "*.php"


find /var/www/php/ -name "*.exe"

There are other arguments for "find", you can find them using "--help" command.

rocky@w3clan:~/websites$ find --help

Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec|time] [path...] [expression]

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:

operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):- 

      -maxdepth LEVELS
      -mindepth LEVELS

tests (N can be +N or -N or N) :-

      -amin N 
      -anewer FILE
      -atime N
      -cmin N
      -cnewer FILE
      -ctime N
      -fstype TYPE
      -gid N
      -group NAME
      -ilname PATTERN
      -iname PATTERN
      -inum N
      -iwholename PATTERN
      -iregex PATTERN
      -links N
      -lname PATTERN
      -mmin N
      -mtime N
      -name PATTERN
      -newer FILE
      -path PATTERN
      -perm [-/]MODE
      -regex PATTERN
      -wholename PATTERN
      -size N[bcwkMG]
      -type [bcdpflsD]
      -uid N
      -used N
      -user NAME
      -xtype [bcdpfls]
      -context CONTEXT


      -printf FORMAT
      -fprintf FILE FORMAT
      -fprint0 FILE
      -fprint FILE
      -fls FILE
      -exec COMMAND ;
      -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ;
      -execdir COMMAND {} + -okdir COMMAND ;

Please see also the documentation at
You can report (and track progress on fixing) bugs in the "find"
program via the GNU findutils bug-reporting page at or, if
you have no web access, by sending email to <>.

We are using here "-type" and "f" :- This is to search for "file" type , if you want to search for directory, you will have to use "-type d" and so on.

Now, as  you can see from above "find" manual, to execute any other command we can use "exec", and for this we are using second command called "sed" we will discuss about it some other time.

but, now "sed" command is searching for "string1" and replacing it with "string2" .

"-i" is ignoring the case sensitivity, and "g" at the end is telling it to perform action globally from top to bottom and not to stop after first finding.

Try it out and let me know, if you have any question.

Loading ...

Related Results :

  1. Find string in (String or Array)
  2. Replace a string in multiple files
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of
  • Any registered user can create related posts based on search term tags.

About the Author