Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-20-2013, 10:20 AM   #1
LQ Newbie
Registered: Apr 2013
Posts: 16

Rep: Reputation: Disabled
find & replace in multiple files

How can I search all *.html files in a directory and it's sub directories for the word "p3nor" and replace them with "p3nis"?
Old 04-20-2013, 11:56 AM   #2
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 525

Rep: Reputation: 141Reputation: 141
See this:
Old 04-20-2013, 01:26 PM   #3
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
If you use the KDE desktop manager, install the krename package and see if that can solve your problem. (GNOME may have a similar package but I don't use GNOME enough to know it's name.)

Also, you could look at the various -exec options of the find command.
Old 04-20-2013, 02:50 PM   #4
LQ Newbie
Registered: Apr 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
thanks for the replies. I found a script on another forum that would do what I'm after:

# *****************************************************************************************
# This script does a recursive, case sensitive directory search and replace of files
# To make a case insensitive search replace, use the -i switch in the grep call
# uses a startdirectory parameter so that you can run it outside of specified directory - else this script will modify itself!
# *****************************************************************************************

# **************** Change Variables Here ************
# **********************************************************

echo "***************************************************"
echo "* Search and Replace in Files Version 01-Aug-2012 *"
echo "***************************************************"


for file in $(grep -l -R $searchterm $startdirectory)
cp $file $file.bak
sed -e "s/$searchterm/$replaceterm/ig" $file > tempfile.tmp
mv tempfile.tmp $file

let i++;

echo "Modified: " $file

echo " *** All Done! *** Modified files:" $i
Old 04-21-2013, 04:36 PM   #5
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

That's not a very good script. In particular it relies on the Don't Read Lines With For error. None of the variables are quoted either, the $i variable is never even used, and you have to hard-code the values into the script.

# **************** Change Variables Here ************
# Alered to use the first 3 arguments by default,
# but will fall back to the entered values if not supplied.

# ***************************************************

while IFS='' read -rd '' fname; do

    sed -i "/$searchterm/ s/$searchterm/$replaceterm/ig" "$fname"
    echo "Modified: $fname"

done < <( grep -R -Z -l "$searchterm" "$startdirectory" )

exit 0
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need help to Find and Replace code in multiple locations troutatious Linux - Newbie 3 01-27-2013 05:30 AM
[SOLVED] Find and replace multiple instances gdavis2287 Programming 23 08-16-2010 12:40 PM
[SOLVED] Find and replace in multiple files in ssh whitelinux Linux - Server 1 07-03-2010 04:43 PM
Find & Replace string in multiple files Rudy Linux - General 14 04-15-2010 08:10 AM
Simple find&replace across 100 files saphil Linux - Newbie 2 01-03-2008 12:21 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:13 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration