Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to know how I can find and replace a specific text in multiple files like in Notepad++ in the linked tutorial.

e.g.: http://cybernetnews.com/find-replace-multiple-files/

share|improve this question
    
It will not have the graphical interface but I would urge you to examine sed (man sed). It is the stream editor that has been in existence from the start of UNIX. –  apolinsky Nov 30 '11 at 14:08

8 Answers 8

up vote 48 down vote accepted

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" {} \;
share|improve this answer
2  
Voted for this because it is such an efficient method –  Sabacon Nov 30 '11 at 16:01
3  
Voted up because sed is the best choice IMO, but answer would be more useful if you explained the components of the answer (personally, I'm not familiar with and curious about the {} \;) –  sequoia mcdowell Nov 30 '11 at 18:50
5  
@sequoiamcdowell Woo! I missed this! Sorry. The braces mean basically "for each match" and the escaped semi-colon (\;) is to prevent double parsing. We don't want the bourne-compatible shell and sed trying to parse the command, just sed. –  user8290 Jul 3 '12 at 22:34
    
@Christopher how does it work if the word contains spaces like cyber net news? How can i convert this to cyber net? –  Khurshid Alam Sep 21 '12 at 18:56
1  
@KhurshidAlam Use the character class with the -e switch: sed -i -e "s/cyber\snet\snews/cyber net/g" –  user8290 Sep 22 '12 at 13:28

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:

Jedit Search In Folder Dialog

This is similar to that of Notepad++ and does the same thing, I believe this is what you want.

share|improve this answer
    
This was perfect for when I was searching for an entire line of code, and was hesitant to escape every single regex-special character if I had done the same thing with sed. –  IQAndreas Sep 7 '13 at 19:47
    
you can replace code blocks (like html, javascript, etc) in seconds. –  lepe Mar 25 at 9:09

Another GUI option is regexxer: regexxer screenshot

share|improve this answer
    
Works perfectly (if you are already familiar with Regex), thank you. –  Ernest Feb 3 at 12:10
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.

share|improve this answer

Check with Geany, it is perfect NPP replacement for Linux. You can do exactly that plus you can use regex.

share|improve this answer
    
I love Geany! It's my favourite editor and is powerful enough. –  Pisu Dec 7 '11 at 10:35
    
As far as I can tell, Geany 1.23.1 (packaged with Ubuntu 13.10) allows searching multiple files using regex, but not replacing in them. –  Drew Noakes Dec 11 '13 at 23:35

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
share|improve this answer

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
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.