Use sed to change string1 to string2 in same file


I wanted to change "/artscrafts/" to "/blog/" in all popup image files (these are not stored in the MT database so search-and-replace inside the Movable Type 4.01 administrative interface doesn't work for those files). The file for each popup image is stored in the same directory as the popup image.

The following worked for me, but there is no guarantee that it will work for you. I am not responsible for any problems you may encounter. Backup all files first.

To change a string of characters to a different string of characters in multiple files:

  1. At a Unix prompt, cd to directory where the files to be modified are located
  2. Type on one line: for f in *php; do sed 's/string1/string2/g' $f > $ && mv $ $f; done

Line 2 means: For every file ending with php, replace string1 with string2 and save the result into a new file, then move the new file over the original file.

For each slash in the string, I needed to use \ in front of it, as follows:

for f in *php; do sed 's/\/artscrafts\//\/blog\//g' $f > $ && mv $ $f; done

Multiple lines can also be used at a Unix prompt as follows:

for f in *php
sed 's/string1/string2/g' $f > $ &&
mv $ $f

Based on information in a thread at dBforums. Note: That code uses the Unix find command to locate files which could be in subdirectories. Back up all files and learn about the find command before using.

for fname in $(find . -name "*" -print)
sed '/word/s/word/new_word/g' ${fname} > ${fname}.new &&
mv ${fname}.new ${fname}


Very useful!

Thank you very much,.


I am trying to compile a script to edit the /etc/login.defs

The file read:


I would like to be able to change it to read


I tried the sed command:
sed 's/PASS_MAX_DAYS */PASS_MAX_DAYS 60/g' /etc/login.defs

the result is this

PASS_MAX_DAYS 60 99999999

How do have it replace the entire line with my input?

I have to admit I am a novice and there may be a simple answer so bear
with me


Hi, Ed - I don't use sed often and am very rusty. You might find these links helpful:

Hi Ed,

you have to 'ESCAPE' the special character - an empty space in this case.

You should do it like this:

sed 's/PASS_MAX_DAYS\ 999999/PASS_MAX_DAYS\ 60/g' /etc/login.defs

If you want to replace everything after the 'PASS_MAX_DAYS' string:

sed 's/PASS_MAX_DAYS.*/PASS_MAX_DAYS\ 60/g' /etc/login.defs

Hope this helps.

When working with paths you can use:
sed 's|/usr/bin|/home/usr/|g'

Leave a comment

Sketch of Cheryl

About Cheryl

Enjoys crocheting, gardening, cats, NASCAR (especially Tony Stewart and Kyle Busch!), reading, photography, snorkeling in Kailua-Kona with sea turtles, Sizzler's Mega Bacon Cheeseburgers, hot and iced decaf coffee, dark chocolate, color (yarn, fabric), playing around with web technologies - not necessarily in that order! Still very much a beginner with quilting, knitting, and sewing. Donates crocheted lap blankets.

List maker, detail-oriented, organized, leans heavily toward perfectionism. ISTJ. Libra.

January 2017

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Disclaimer: This is a personal website - my place of self expression. The views expressed are mine alone and do not necessarily reflect the views of my employer or any other organization I am a member of or affiliated with.
Creative Commons License
This blog is licensed under a Creative Commons License.
Powered by Movable Type 5.2.2

About this Entry

This page contains a single entry by Cheryl published on October 22, 2007 12:19 PM.

Redirect URLs with underscores to URLs with dashes (hyphens) was the previous entry in this blog.

Test reCaptcha for comments is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Cat playing with strand of yarn.


Recent Comments

  • Jerry: When working with paths you can use: sed 's|/usr/bin|/home/usr/|g' read more
  • jetpac: Hi Ed, you have to 'ESCAPE' the special character - read more
  • Cheryl: Hi, Ed - I don't use sed often and am read more
  • Ed: I am trying to compile a script to edit the read more
  • Herman Jaramillo: Very useful! Thank you very much,. Herman. read more