Mercurial > ec-dotfiles
annotate bin/prename @ 536:d4e874f04bc6
Set tab space to 4. Just because.
author | edogawaconan <me@myconan.net> |
---|---|
date | Mon, 05 Jan 2015 21:33:35 +0900 |
parents | f6d29c03142f |
children | 0d928126fcf6 |
rev | line source |
---|---|
397
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
1 #!/usr/bin/perl -w |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
2 # |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
3 # This script was developed by Robin Barker (Robin.Barker@npl.co.uk), |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
4 # from Larry Wall's original script eg/rename from the perl source. |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
5 # |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
6 # This script is free software; you can redistribute it and/or modify it |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
7 # under the same terms as Perl itself. |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
8 # |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
9 # Larry(?)'s RCS header: |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
10 # RCSfile: rename,v Revision: 4.1 Date: 92/08/07 17:20:30 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
11 # |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
12 # $RCSfile: rename,v $$Revision: 1.5 $$Date: 1998/12/18 16:16:31 $ |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
13 # |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
14 # $Log: rename,v $ |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
15 # Revision 1.5 1998/12/18 16:16:31 rmb1 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
16 # moved to perl/source |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
17 # changed man documentation to POD |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
18 # |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
19 # Revision 1.4 1997/02/27 17:19:26 rmb1 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
20 # corrected usage string |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
21 # |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
22 # Revision 1.3 1997/02/27 16:39:07 rmb1 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
23 # added -v |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
24 # |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
25 # Revision 1.2 1997/02/27 16:15:40 rmb1 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
26 # *** empty log message *** |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
27 # |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
28 # Revision 1.1 1997/02/27 15:48:51 rmb1 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
29 # Initial revision |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
30 # |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
31 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
32 use strict; |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
33 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
34 use Getopt::Long; |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
35 Getopt::Long::Configure('bundling'); |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
36 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
37 my ($verbose, $no_act, $force, $op); |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
38 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
39 die "Usage: rename [-v] [-n] [-f] perlexpr [filenames]\n" |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
40 unless GetOptions( |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
41 'v|verbose' => \$verbose, |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
42 'n|no-act' => \$no_act, |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
43 'f|force' => \$force, |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
44 ) and $op = shift; |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
45 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
46 $verbose++ if $no_act; |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
47 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
48 if (!@ARGV) { |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
49 print "reading filenames from STDIN\n" if $verbose; |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
50 @ARGV = <STDIN>; |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
51 chop(@ARGV); |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
52 } |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
53 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
54 for (@ARGV) { |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
55 my $was = $_; |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
56 eval $op; |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
57 die $@ if $@; |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
58 next if $was eq $_; # ignore quietly |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
59 if (-e $_ and !$force) |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
60 { |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
61 warn "$was not renamed: $_ already exists\n"; |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
62 } |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
63 elsif ($no_act or rename $was, $_) |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
64 { |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
65 print "$was renamed as $_\n" if $verbose; |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
66 } |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
67 else |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
68 { |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
69 warn "Can't rename $was $_: $!\n"; |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
70 } |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
71 } |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
72 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
73 __END__ |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
74 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
75 =head1 NAME |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
76 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
77 rename - renames multiple files |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
78 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
79 =head1 SYNOPSIS |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
80 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
81 B<rename> S<[ B<-v> ]> S<[ B<-n> ]> S<[ B<-f> ]> I<perlexpr> S<[ I<files> ]> |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
82 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
83 =head1 DESCRIPTION |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
84 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
85 C<rename> |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
86 renames the filenames supplied according to the rule specified as the |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
87 first argument. |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
88 The I<perlexpr> |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
89 argument is a Perl expression which is expected to modify the C<$_> |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
90 string in Perl for at least some of the filenames specified. |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
91 If a given filename is not modified by the expression, it will not be |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
92 renamed. |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
93 If no filenames are given on the command line, filenames will be read |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
94 via standard input. |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
95 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
96 For example, to rename all files matching C<*.bak> to strip the extension, |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
97 you might say |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
98 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
99 rename 's/\.bak$//' *.bak |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
100 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
101 To translate uppercase names to lower, you'd use |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
102 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
103 rename 'y/A-Z/a-z/' * |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
104 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
105 =head1 OPTIONS |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
106 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
107 =over 8 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
108 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
109 =item B<-v>, B<--verbose> |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
110 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
111 Verbose: print names of files successfully renamed. |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
112 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
113 =item B<-n>, B<--no-act> |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
114 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
115 No Action: show what files would have been renamed. |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
116 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
117 =item B<-f>, B<--force> |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
118 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
119 Force: overwrite existing files. |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
120 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
121 =back |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
122 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
123 =head1 ENVIRONMENT |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
124 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
125 No environment variables are used. |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
126 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
127 =head1 AUTHOR |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
128 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
129 Larry Wall |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
130 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
131 =head1 SEE ALSO |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
132 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
133 mv(1), perl(1) |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
134 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
135 =head1 DIAGNOSTICS |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
136 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
137 If you give an invalid Perl expression you'll get a syntax error. |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
138 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
139 =head1 BUGS |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
140 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
141 The original C<rename> did not check for the existence of target filenames, |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
142 so had to be used with care. I hope I've fixed that (Robin Barker). |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
143 |
f6d29c03142f
Added perl rename from debian system.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
144 =cut |