Skip Navigation
Site HomeSearch

On-Line Reference:
RedHat Linux Unleashed

Did this help?
... or
Donate at PayPal®...

Tux the Pengin

OK, here's a freebie: A simulation of the old text-based Star Trek game we used to play on HP 2100's using a Teletype ASR-33 as a console: StarTrek Game

Linux Find and Replace

Why edit a bunch of files individually?

Use commandline looping logic and Linux's built-in utility, the "sed" command. Here's the command:

% cd directorypath
% for f in filespec
> do mv $f $f.old
> sed 's#findstring#replacestring#g' $f.old > $f
> rm -f $f.old
> done


First, change directories to the directory containing the files you want to modify, with the standard command, cd directorypath

The filespec is a standard Linux commandline expression to select which files to search. Typical expressions might be:

*            {all files}
*.php        {all files ending in '.php'}
*.html       {all html files}

Set findstring to the text string you want to find in each target file, and replacestring with the text to substitute in its place.

Normally, search expressions use slashes "/" to bracket the text strings, as in /findstring/replacestring/. Note that in the example I've used "#" as the string delimiters; this was to allow me to use a findstring that includes slashes such as http://hostdomain/subdir/.

Print plain