LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



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

Rep: Reputation: 30
perl renaming files


Hi

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, 05:43 PM   #2
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
Cool

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

Code:
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.

Code:
#!/usr/bin/perl
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, 10:27 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

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

Code:
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
 
  


Reply

Tags
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 06:07 AM
perl files download instead of parsed on apache2 mod-perl2 not parsing perl files zeigerpuppy Debian 1 02-16-2006 06:31 AM
renaming files TomalakBORG Linux - Newbie 4 12-24-2005 11:14 AM
Renaming files in one go saurya_s Linux - Software 1 01-12-2004 02:16 PM
renaming ENV hash in Perl nbp Programming 4 11-01-2003 04:07 AM


All times are GMT -5. The time now is 06:50 PM.

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