LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 04-20-2013, 10:20 AM   #1
chonk0
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
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
See this: http://www.linuxquestions.org/questi...ng-4175453776/
 
Old 04-20-2013, 01:26 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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
chonk0
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:


# *****************************************************************************************
# find_and_replace_in_files.sh
# 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!
# *****************************************************************************************

!/bin/bash
# **************** Change Variables Here ************
startdirectory="/your/start/directory"
searchterm="test"
replaceterm="test=ok!"
# **********************************************************

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

i=0;

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

let i++;

echo "Modified: " $file
done

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: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

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

startdirectory="${1:-/your/start/directory}"
searchterm="${2:-test}"
replaceterm="${3:test=ok!}"
# ***************************************************

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.
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:38 PM.

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