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"?
|
|
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. |
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 |
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 |
All times are GMT -5. The time now is 10:55 AM. |