Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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

10 Answers 10

up vote 79 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
Voted for this because it is such an efficient method – Sabacon Nov 30 '11 at 16:01
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
@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
@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 '14 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 '14 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
You can search and replace in Geany multiple files if you open them all and then using the "in session" button in the Search/Replace dialog. – MV. May 3 at 22:58

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:

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

You can use this script, copy code and make a file

I have modified it a little; please tell me your opinion.

# *****************************************************************************************
# 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
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

A very simple solution: replace in all *.txt files in folder string_1 with string_2:

sed -i 's/string_1/string_2/g' *.txt
share|improve this answer
find . -name "*.txt" |xargs sed -i "s/searched_Text/replacement_Text/g"

works for me on fedora

share|improve this answer
You could add a line explaining how this improves over – muru Sep 19 '14 at 0:30
@muru It runs much fewer sed instances! So it is much faster in terms of CPU time. Because the file access time would be limiting, I think it's not much faster in clock time, but gives lower system load. Note xargs will put lots, like hundereds, of filenames on the commandline of a single sed command - filling the available buffer size based on the length of the paths. – Volker Siegel Sep 19 '14 at 1:07
Hmm... if this gets closed, I could replace it with one clearly explaining why it's better than the upvoted one. – Volker Siegel Sep 19 '14 at 1:08
@VolkerSiegel that can be achieved with + instead of \; in find, right? – muru Sep 19 '14 at 1:21
@muru Possibly... My "rigth tool for the job"-instinct clearly prefers xargs, but you're right; from man find: -exec command {} +: ...The command line is built in much the same way that xargs.... Makes me think of command lines of git filter-branch... ;) – Volker Siegel Sep 19 '14 at 1:26

protected by Community Apr 17 at 12:54

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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