-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename
executable file
·47 lines (34 loc) · 1.01 KB
/
rename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/perl -w
# Usage: rename perlexpr [files]
use strict;
(my $op = shift) || die "Usage: $0 perlexpr [filenames]\n";
if (!@ARGV) {
@ARGV = <STDIN>;
chop(@ARGV);
}
for (@ARGV) {
my $was = $_;
eval $op;
die $@ if $@;
rename($was,$_) unless $was eq $_;
}
sub usage{
while(<DATA>){
print $_;
}
}
__END__
For example, to rename all files matching *.bak to strip the extension, you might say:
% rename 's/\.bak$//' *.bak
But you're not limited to simple substitutions - you have at your disposal the full expressive power of Perl. To add those extensions back on, for instance, say this:
% rename '$_ .= ".bak"' *
or even:
% rename 's/$/.bak/' *
To translate uppercase names to lowercase, you'd use:
% rename 'tr/A-Z/a-z/' *
And how about these?
% rename 's/foo/bar/; $_ = $was if -e' *foo*
% find . -print | rename 's/readme/README/i'
% find . -print | rename 's/$/.old/ if -M $_ > 0.5'
% find . -name '*,v' -print | \
rename 's#(.*)/#$1/RCS/#, $x{$1}++ || mkdir("$1/RCS", 0777)'