<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.andreas-duffner.de/index.php?action=history&amp;feed=atom&amp;title=Using_sed_recursive_on_a_file_system</id>
	<title>Using sed recursive on a file system - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.andreas-duffner.de/index.php?action=history&amp;feed=atom&amp;title=Using_sed_recursive_on_a_file_system"/>
	<link rel="alternate" type="text/html" href="http://wiki.andreas-duffner.de/index.php?title=Using_sed_recursive_on_a_file_system&amp;action=history"/>
	<updated>2026-04-09T12:00:58Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.17</generator>
	<entry>
		<id>http://wiki.andreas-duffner.de/index.php?title=Using_sed_recursive_on_a_file_system&amp;diff=130&amp;oldid=prev</id>
		<title>Andreas: Created page with &quot;== Best solution for me so far == use the following phase, do not put it into a script, just replace the 4 variables.  When I put it into a script I sometimes get weird behavi...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wiki.andreas-duffner.de/index.php?title=Using_sed_recursive_on_a_file_system&amp;diff=130&amp;oldid=prev"/>
		<updated>2016-02-16T11:43:04Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== Best solution for me so far == use the following phase, do not put it into a script, just replace the 4 variables.  When I put it into a script I sometimes get weird behavi...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Best solution for me so far ==&lt;br /&gt;
use the following phase, do not put it into a script, just replace the 4 variables. &lt;br /&gt;
When I put it into a script I sometimes get weird behaviour, depending on the search pattern.&lt;br /&gt;
 sed -i s/$SEARCH/$REPLACE/g  `grep -irl $SEARCH * --include=$PATTERN`&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== simple change of multiple files, even in multiple directories ==&lt;br /&gt;
If you want to use sed on many files you always should first try to do it the easy way:&lt;br /&gt;
 sed -i &amp;#039;s/lookingFor/replacingWith/g&amp;#039; */*.c # many files in many directories&lt;br /&gt;
&lt;br /&gt;
But if you have to have more control there are two ways I currently know of:&lt;br /&gt;
== let find do the searching ==&lt;br /&gt;
 find . -type f -exec sed -i &amp;#039;s/lookingFor/replacingWith/g&amp;#039; {} +&lt;br /&gt;
find will iterate all directories below the given &amp;quot;.&amp;quot; and call the given command for each file. {} will be replaced with the filenames.&lt;br /&gt;
The + tells find to give many files at the same time to the given command.&lt;br /&gt;
I do not really understand the use of + here, the manpage does not use it in its examples. But it works. So currently I&amp;#039;d keep it. If I would not use the second solution.&lt;br /&gt;
Beware of problems with {} when the filenames may contain spaces. &lt;br /&gt;
Perhaps you&amp;#039;ll have to do put something like \&amp;#039; arround them, not sure if there even is a problem. Just a hint.&lt;br /&gt;
&lt;br /&gt;
== let grep find the files and then use sed on these files ==&lt;br /&gt;
 sed -i &amp;#039;s/lookingFor/replacingWith/g&amp;#039;  `grep -ril &amp;#039;lookingFor&amp;#039; * --include=*.cpp`&lt;br /&gt;
You will have to use ticks around the grep command !&lt;br /&gt;
The disadvantage of this is, that you&amp;#039;ll have to give the &amp;#039;lookingFor&amp;#039; twice.&lt;br /&gt;
But you could use a script too... :-)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# replace-in-files&lt;br /&gt;
if [ &amp;quot;$1&amp;quot; = &amp;quot;--help&amp;quot; ] || [ $# -ne 4 ]; then&lt;br /&gt;
    echo replace-in-files v 0.1, it@andreas-duffner.de&lt;br /&gt;
    echo this file will replace text in given files&lt;br /&gt;
    echo Syntax: $0 \&amp;lt;dir\&amp;gt; \&amp;lt;lookingFor\&amp;gt; \&amp;lt;replaceWith\&amp;gt; \&amp;lt;filePattern\&amp;gt;&lt;br /&gt;
    echo Example: $0 . Bug Feature &amp;#039;*.cpp&amp;#039;&lt;br /&gt;
    exit 1&lt;br /&gt;
fi  &lt;br /&gt;
&lt;br /&gt;
DIR=$1&lt;br /&gt;
SEARCH=$2&lt;br /&gt;
REPLACE=$3&lt;br /&gt;
PATTERN=$4&lt;br /&gt;
&lt;br /&gt;
sed -i s/$SEARCH/$REPLACE/g  `grep -irl $SEARCH * --include=$PATTERN`&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Andreas</name></author>
	</entry>
</feed>