Where art thou 'rename'
Paul Johnson
pauljohn32 at gmail.com
Tue Aug 5 18:59:12 UTC 2008
On Tue, Aug 5, 2008 at 1:41 PM, Romain Kang <romain at kzsu.stanford.edu> wrote:
> Here's something posted to USENET ca. 1991. I've been carrying around
> my own copy ever since:
>
> #! /usr/local/bin/perl
> $op = shift;
> for (@ARGV) {
> $was = $_;
> eval $op;
> die $@ if $@;
> rename($was,$_) unless $was eq $_;
> }
>
> --
I've been using this same scritp since 1995. It is from the Unix
Powertools examples, I think. Anyway, my version has some usage
examples at the top. I don't think people could figure it out without
them. If you know perl or sed s/before/after/ this will be familiar
to you.
###########################
#!/usr/bin/perl
#
# rename script examples from lwall:
# rename 's/\.orig$//' *.orig
# rename 'y/A-Z/a-z/ unless /^Make/' *
# rename '$_ .= ".bad"' *.f
# rename 'print "$_: "; s/foo/bar/ if <stdin> =~ /^y/i' *
$op = shift;
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was,$_) unless $was eq $_;
}
##########################
Here are some examples I regularly do after downloading things
## replace spaces with _ in all file names ending in mp3
rename s/\ /_/g *.mp3
(recall the g means "for all instances". Without it, it only does first one).
## Change " - " to "-"
rename s/\ - \ /-/ g *
## THis accepts regular expressions, so you can use ^ and $ in a handy way.
### Insert "My" at beginning of all file names
rename s/^/My/ *
### Add "-orig" to end of all file names
rename s/$/-orig/ *
Anything you can do with Perl/sed and regex
--
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas
More information about the ubuntu-users
mailing list