LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 03: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, 04:43 PM   #2
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
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, 09:27 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:11 AM.

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
Open Source Consulting | Domain Registration