<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ron</title>
    <description>The latest articles on DEV Community by Ron (@itwerx).</description>
    <link>https://dev.to/itwerx</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2760678%2Ffed41ba8-2367-47e9-ac01-a1a9148f73f5.png</url>
      <title>DEV Community: Ron</title>
      <link>https://dev.to/itwerx</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/itwerx"/>
    <language>en</language>
    <item>
      <title>How to change a word across multiple files in a directory using find and sed</title>
      <dc:creator>Ron</dc:creator>
      <pubDate>Sat, 25 Jan 2025 20:00:35 +0000</pubDate>
      <link>https://dev.to/itwerx/how-to-change-a-word-across-multiple-files-in-a-directory-using-find-and-sed-1cod</link>
      <guid>https://dev.to/itwerx/how-to-change-a-word-across-multiple-files-in-a-directory-using-find-and-sed-1cod</guid>
      <description>&lt;p&gt;If you need to replace or update a word in multiple files within a directory, you can use command-line tools like find and sed to accomplish the task efficiently. Below is a method that demonstrates how to locate files, extract specific parts of their names, and make the desired changes.&lt;/p&gt;

&lt;p&gt;Step-by-Step Command Explanation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
find ./directory -type f -name "*.filename.yml" -exec sh -c '

 for file; do

   # Extract the base name of the file without the extension
   what_you_want_to_change_to=$(basename "$file" .filename.yml)

   # Use sed to add a new line containing the replacement word
   sed -i "/name: $what_you_want_to_change_to/a replaces: '\''replace_me'\''" "$file"

 done

' sh {} +
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key Components of the Command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;find ./directory&lt;/code&gt;: Searches within the specified directory.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-type f&lt;/code&gt;: Targets only files (ignores directories).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-name "*.filename.yml"&lt;/code&gt;: Filters files based on their extension or pattern.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$(basename "$file" .filename.yml)&lt;/code&gt;: Extracts the file name without its extension.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sed -i&lt;/code&gt;: Edits the files in place, adding the new content after the matched line.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;replaces: '\''replace_me'\''&lt;/code&gt;: Appends a line in the format replaces: &lt;code&gt;'replace_me'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Use Case&lt;/strong&gt;&lt;br&gt;
Suppose you have several .yml files, each containing a line like name: example. This script finds each file, extracts the file name, and appends a new line with a replacement value.&lt;/p&gt;

&lt;p&gt;The final result in each file will look like this:&lt;br&gt;
name: example&lt;br&gt;
replaces: 'replace_me'&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Use find and sed?&lt;/strong&gt;&lt;br&gt;
This method is efficient for batch processing multiple files and eliminates the need for manual editing. By combining find, a shell script, and sed, you gain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Speed: Processes hundreds of files in seconds.&lt;/li&gt;
&lt;li&gt;Precision: Only modifies files matching your criteria.&lt;/li&gt;
&lt;li&gt;Scalability: Easily adaptable to different patterns and use cases.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
