I want search for all occurrences of:


In the current directory and its subdirectories and replace it with:

<input type="hidden" name="_token" value="<cfoutput>#STOKEN#</cfoutput>"/>

I tried the following on one file. The file was touched -- last modified date was changed -- but the replacement was not made:

sed -i -re 's/\<\/form\>/\<input type="hidden" name="_token" value="\<cfoutput\>#STOKEN#\<\/cfoutput\>"\>\<\/form\>/gi' ./math.cfm

What am I missing? Is there a better command to use for this?


The \< and \> elements have special meaning in sed regex (as word boundaries).

Don't make things hard by using / as sed's separator and trying to get the escaping right - choose an alternate delimiter that doesn't appear in your text, such as % or |.

The i (insert) command seems to me to be a more natural choice than the s command for this task: you could try

'\%</form>% i\<input type="hidden" name="_token" value="<cfoutput>#STOKEN#</cfoutput>"/>'

To apply such a command to subdirectories recursively, you can supply the sed command as a -exec or -execdir argument to find e.g.

find path/to/dir -type f -name '*.cfm' -execdir sed -i -re '\%</form>% i\<input type="hidden" name="_token" value="<cfoutput>#STOKEN#</cfoutput>"/>' {} +

Alternatively, you could use the shell's globstar functionality.

  • Awesome! That did it; thanks! I can specify the files I want to change in the current folder by replacing math.cfm with *.cfm. How would I extend this to subfolders? – user3558931 Jul 2 '15 at 15:10
  • @PeterKA please see updated answer – steeldriver Jul 2 '15 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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