If you're on a unix-like system, you can use plain old find to find all files in this directory or subdirectories, then use perl with "-i" and "-p" to operate on each of them:

find . -type f \! -name '*.bak' -print0 | \ xargs -0 perl -i.bak -plwe 's/foo/bar/g'

Modify the arguments to find as appropriate. This is especially nice if you are only performing this substitution on ".html" or ".txt" files, as that removes the need to check for ".bak" files. For example, if you only want to edit files ending with ".html":

find . -type f -name '*.html' -print0 | \ xargs -0 perl -i.bak -plwe 's/foo/bar/g'

Also, some older / vendor-supplied versions of find and xargs don't have -print0 and -0; you can just use -print on the find, and remove the -0 from the xargs argument list. (They are there for added security, in case you have files with newlines in their names.)

If you are on a non-unix system, the File::Find approaches should work. (Or you can instally Cygwin etc.)

Finally, the details of the perl flags there are described in perlrun; the important one here is "-i.bak", which makes a copy of each file named on the command line then modifies the original in-place (or so you can consider it; more likely, it renames the original to the backup filename, then reads from the backup, operates on the contents, then writes to the original filename).

So, this perl command:

perl -i.bak -plwe 's/foo/bar/g' file1.txt file2.txt file3.txt

Is roughly equivalent to this shell loop:

for i in file1.txt file2.txt file3.txt do mv $i $i.bak perl -plwe 's/foo/bar/g' < $i.bak > $i done

(Yes, I know that the input redirection is unnecessary there; I left it in so that it is more obvious that perl is being used as a filter.)


In reply to Re: Search & Replace in subdirectory files by tkil
in thread Search & Replace in subdirectory files by Rina

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.