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

I have several files that I'd like to change extensions of in one fell swoop.
like, if i have all these .bob files and want to change them to .fred files, is there a way to do this with perl?

Replies are listed 'Best First'.
Re: changing file extensions
by abstracts (Hermit) on Aug 03, 2001 at 03:11 UTC
    Hello,

    perl -we '/(.*)\./&&rename"$1.foo","$1.bar"for<*.foo>'
    Probably not the best way (2 foos), but it works.

    Hope this helps,,,

    Aziz,,,

Re: changing file extensions (boo)
by boo_radley (Parson) on Aug 03, 2001 at 03:05 UTC
    update

    Soy un perdedor

      There is a rename function in Perl. Who needs a module?

        Not that it seems to be an issue in this particular case, but rename may have issues with moving files across filesystems, etc., which is why File::Copy is standard with the distribution.

        And if you wanted a recursive Perl-y solution, you'dn use File::Find.

        pedantically,

        perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: changing file extensions
by John M. Dlugosz (Monsignor) on Aug 03, 2001 at 03:17 UTC
    What OS and shell? I wouldn't even use Perl. I'd just say "rename *.bob *.fred" on the command line. Or, if it has subdirs, "global rename *.bob *.fred".

      rename from to files... on *nix.

      $ rename .bob .fred *.bob

      After Compline,
      Zaxo

      Hello

      "I wouldn't even use Perl. I'd just say "rename *.bob *.fred" on the command line."

      But you are using Perl, since rename is written in Perl :-)

      Aziz,,,

      Update:Many people seem to vote without reading the followups, so, here it is:

      RENAME(1) Perl Programmers Reference Guide RENAME(1) NAME rename - renames multiple files SYNOPSIS rename [ -v ] perlexpr [ files ] DESCRIPTION "rename" renames the filenames supplied according to the rule specified as the first argument. The perlexpr argu? ment is a Perl expression which is expected to modify the "$_" string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input. For example, to rename all files matching "*.bak" to strip the extension, you might say rename 's/\e.bak$//' *.bak To translate uppercase names to lower, you'd use rename 'y/A-Z/a-z/' * [snip] AUTHOR Larry Wall
      Aziz,,,
        No, it's a shell intrinsic command.
        [D:\Program Files\4nt]rename /? Rename files or subdirectories. RENAME [/A:[[-]rhsda] /E /I"text" /NPQST] old_name... new_name /A:(ttribute select) /P(rompt) /E (no error messages) /Q(uiet) /I (match description) /S(ubdirectory) /N(othing) /T(otal) [D:\Program Files\4nt]