Had a boss show me this one once and it really opened my eyes up. I just never paid attention to those "other" switches you could add to the shebang. What he showed me is a way to combine a two line perl script to a find to change every single file in a directory structure at once (The actual scenario was we had a client lose their hostname through shear incompetence and we had to compensate with what they could get from internic - and we needed to change every file in their web site simultatneously because they also had a practice of hardocding URL's into their site - if it isn't complex, it isn't life...).

OK, here's the code, followed by the explanation...

#!/usr/bin/perl -pi $_ =~ s/sometext/someothertext/mg;

Yeah, that's the whole script. The secret is in the -pi. Check out the perldoc perl pages for more information - this form replaces the page entriley. You can also set it so perl writes a backup file as it goes too. To use the script, just do a find exec, like so:

find ./ -name "*" -type f -exec ./somescript.pl {} \;

Believe me, this has been a lifesaver. Like moving netscape server instances between servers so you don't have to retweak....



Zed? Zed's dead, baby, Zed's dead.

In reply to Need to change, oh, every file at once? by z3d

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.