Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Automatically Rewrite Shebang Line on Multiple Files

by spickles (Scribe)
on Feb 11, 2009 at 21:35 UTC ( #743170=perlquestion: print w/replies, xml ) Need Help??

spickles has asked for the wisdom of the Perl Monks concerning the following question:

Before I write my own script, I was wondering if anyone has already written a script to parse a directory looking for .pl and .cgi files and change the shebang line? I've got an installation of xampp and perl that I want to move from my c:\ drive to a portable drive. So I was thinking I would parse the files in a directory that have a shebang line and re-write it to point to the new location.

  • Comment on Automatically Rewrite Shebang Line on Multiple Files

Replies are listed 'Best First'.
Re: Automatically Rewrite Shebang Line on Multiple Files
by samtregar (Abbot) on Feb 11, 2009 at 21:39 UTC
    This is very easy if you've got cygwin to give you a working shell, find and xargs. For example, to change from /usr/bin/perl to /usr/local/bin/perl I'd do something like (untested):

    find -name '*.pl' -o -name '*.cgi' | xargs perl -pi -e "s{^#!/usr/bin/perl}{#!/usr/local/bin/perl}"

    The -pi command-line switch tells Perl to do in-place editing and the -e specifies the code to on each line. If your filenames might have spaces in them you can add -print0 to the find call and -0 to the xargs call before perl.

    If you want to do it all in Perl can use File::Find or something similar to find the files for you.


      for %q in (*.cgi *.pl) do ( perl -pi.bak -e"s{^#!/usr/bin/perl}{#!/usr/local/bin/perl}" "%q" )

      Change for to for /r for a recursive solution.

      Update: Simplified solution

        Your solution with a for loop has the word perl within it. Is this a script in a language other than perl, or is there some significance to the use of "perl -pi.bak"? I'm not familiar with cygwin - the last time I tried to install it something didn't work right.


      Hi, possibly just a typo, the shebang starts with #!, not with !#.

        Thanks - there's probably other errors. I usually have to play with a one-liner like this before it actually works.


Re: Automatically Rewrite Shebang Line on Multiple Files
by linuxer (Curate) on Feb 11, 2009 at 22:28 UTC

    Non Perl solution; tested on linux; I think this should be usable in cygwin as well:

     find . -iname "*.pl" -o -iname "*.cgi"  | xargs sed -i.bac -e '1s"^#!.\+$"#! /usr/bin/perl"'


    • added address 1 to sed expression, so only first line is examined/changed
Re: Automatically Rewrite Shebang Line on Multiple Files
by repellent (Priest) on Feb 12, 2009 at 06:19 UTC
    I'd just like to point out that only the first line is of significance for a shebang line. Hence, performing a search-and-replace for lines $. > 1 is not necessary unless... unless you have script-embedded shebangs?!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://743170]
Approved by samtregar
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2020-08-15 08:49 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (78 votes). Check out past polls.