I want to know how I can find and replace a specific text in multiple files like in Notepad++ in the linked tutorial.
10 Answers
Here I use sed to replace every occurrence of the word "cybernetnews" with "cybernet" in every file with the extension, c, in the directory, /home/user/directory/.
find /home/user/directory -name \*.c -exec sed -i "s/cybernetnews/cybernet/g" {} \;
A more generic variation where you search recursively from the directory of execution and operate on only regular, readable, writeable files:
find ./ -type f -readable -writable -exec sed -i "s/cybernetnews/cybernet/g" {} \;
 
    
    - 123
The stream editor,sed, is a powerful utility for this kind of work and is my first choice, however, if you want to do this from an ordinary text editor using an Ubuntu based native application, I would suggest you take a look at Jedit, It is available in the repositories and can be installed by typing in your console:
sudo apt-get install jedit
Start jedit, click the search menu item, in the menu list, click the Search in Directory item, you will be presented with the dialog below:

This is similar to that of Notepad++ and does the same thing, I believe this is what you want.
 
    
    - 41,474
perl -pi -e 's/oldtext/newtext/g' *
replaces any occurence of oldtext by newtext in all files in the current folder. However you will have to escape all perl special characters within oldtext and newtext using the backslash.
 
    
    - 14,338
Check with Geany, it is perfect NPP replacement for Linux. You can do exactly that plus you can use regex.
 
    
    - 73,717
 
    
    - 2,798
You can use this script, copy code and make a file find_and_replace_in_files.sh.
I have modified it a little; please tell me your opinion.
# *****************************************************************************************
# 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
 
    
    - 119,640
 
    
    - 41
find . -name "*.txt" |xargs sed -i "s/searched_Text/replacement_Text/g"
works for me on fedora
 
    
    - 207,228
 
    
    - 31
- 1
I wrote a little script for just this thing. If you only need the basics and are not familiar with sed etc, take a look here: http://www.csrdu.org/nauman/2010/12/30/bash-script-to-find-and-replace-in-a-set-of-files/
The script is the following:
for f in submit_*;
  do sed "s/old_db_name/new_db_name/" < $f > a_$f ;
  mv a_$f $f ;
done
 
    
    - 1,057
Another program is Searchmonkey.
SearchMonkey is a light-weight Gtk application that aims to replace the cumbersome find/grep with a slick user interface that quickly provides a mark-up showing locations and quantity of text matches. The goal is to provide a simple to use and accessible search tool for end-users, and software developers alike.
 
    
    - 5,878
 
    
    - 6,397
 
     
    
 
     
    