Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-24-2006, 03:17 PM   #1
Registered: May 2003
Location: NYC
Distribution: CentOS
Posts: 261

Rep: Reputation: 30
perl renaming files


Does anyone know how you can change file extensions using perl on the fly. (Ex: change a.txt to a.TXT)

NOTE: I prefer not to use sed or awk if possible.

Thanks in advance
Old 04-24-2006, 04:43 PM   #2
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30

This is a script I wrote and use all the time. It allows you to use a regex to rename files. For example, if you wanted to anything with extension .TXT to .txt, you would do

pmv 's/TXT$/txt$/' *.TXT
And here's the script, I call it pmv (perl mv). It's pretty much right out of Programming Perl with a little embellishment.

use warnings;
use strict;

my ($verbose, $test);
my $separator = " => ";

GetOptions (
	    "test|dryrun" => \$test,
	    "separator=s" => \$separator,
	    "verbose" => \$verbose,
	   ) or die "$0: options error\n";

$verbose = 1 if $test;

my $op = shift or die "Usage: $0 REGEX file1 [ file2 ... ]\n";

for (@ARGV) {
    my $was = $_;
    eval $op;
    die "Invalid regex" if $@;
    unless ($was eq $_) {
      if (not $test) {
	rename $was, $_ or warn "Rename of $was failed";
      print "$was$separator$_\n" if $verbose;
Old 04-25-2006, 09:27 AM   #3
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,360

Rep: Reputation: 192Reputation: 192
or I usually do stuff this way,

ls *.txt | while read f;do echo mv $f ${f%.txt}.TXT;done  > 1
(have a look at '1' see if's groovy, you also get a record of what you did)

sh 1


perl, renaming

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Another renaming files Perl script STAGED Programming 6 03-04-2006 05:07 AM
perl files download instead of parsed on apache2 mod-perl2 not parsing perl files zeigerpuppy Debian 1 02-16-2006 05:31 AM
renaming files TomalakBORG Linux - Newbie 4 12-24-2005 10:14 AM
Renaming files in one go saurya_s Linux - Software 1 01-12-2004 01:16 PM
renaming ENV hash in Perl nbp Programming 4 11-01-2003 03:07 AM

All times are GMT -5. The time now is 01:37 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration