In Unix, how can I replace a single string in a large number of files?

In Unix, there are two ways to replace a single string in a large number of files, depending on whether or not you have Perl installed on your system.

If you have Perl installed on your system

With Perl, you can make the replacement from the Unix command line prompt. At the prompt, enter:

  perl -pi -e 's/old_string/new_string/g' file_pattern

Replace old_string with the string you want to replace and new_string with the replacement string. Replace file_pattern with the files you want to modify. This can be a shell wildcard, such as *.html. The search is case sensitive and Perl will replace substrings of words. For example, take the following command:

  perl -pi -e 's/temp/tmp/g' *.html

Perl would change every instance of temp it found into tmp (including instances of temporary; it would change them to tmporary because of the temp string in the beginning of the word), but would not change the word TEMP into tmp. If the string has characters Perl or the shell would normally interpret as commands, such as / (a forward slash), < (a less-than symbol), > (a greater-than symbol), or ; (a semicolon), put \ (a backslash) before the character. Take the following replacement string, for example:

  /happy/

To correctly replace it, you would have to escape the forward slashes with the escape character (the backslash) so that the forward slashes aren't interpreted as commands. Your replacement string would need to be:

  \/happy\/

If you don't have Perl installed on your system

If you don't have Perl installed on your system, you can also create a shell script using the sed command. Use an editor such as Emacs, Pico, or vi to write the shell script. The sed command outputs the changes to temporary files, then the script replaces the files containing the old string with the temporary files, which have the new string. A sample script follows:

   #!/bin/sh
   myname="/tmp/`whoami``date +%d%m%H%M%S`"
   if test -f $myname
   then
     echo "$0: Cannot make directory $myname (already exists)" 1&>2
     exit 0
   fi
   mkdir "$myname"
   for FILE in $@; do
   sed 's/old_string/new_string/g' $FILE > "$myname"/"$FILE"new_tmp
   mv "$myname"/"$FILE"new_tmp $FILE
   done
   rmdir $myname

Replace old_string with the string you want to replace and new_string with the replacement string.

Note: This script makes use of the /tmp directory.

The sed command uses the same syntax as Perl to search for and replace strings. Once you have created the script, enter the following at the Unix command line prompt:

  sh script_name file_pattern

Replace script_name with the filename of the script, and file_pattern with the file or files you want to modify. You can specify the files that you want to modify by using a shell wildcard, such as *.html.

Here are examples where the script is named replaceit. In the first case, there is a single file called afile to modify; in the second, the files to be modified are all those with .c as a suffix.

  sh replaceit afile

  sh replaceit *.c

At Indiana University, for personal or departmental Linux or Unix systems support, see At IU, how do I get support for Linux or Unix?

This is document afrk in the Knowledge Base.
Last modified on 2008-08-22.

  • Fill out this form to submit your issue to the UITS Support Center.
  • Please note that you must be affiliated with Indiana University to receive support.
  • All fields are required.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.

  • Fill out this form to submit your comment to the IU Knowledge Base.
  • If you are affiliated with Indiana University and need help with a computing problem, please use the I need help with a computing problem section above, or contact your campus Support Center.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.