3 # This script was developed by Robin Barker (Robin.Barker@npl.co.uk),
4 # from Larry Wall's original script eg/rename from the perl source.
6 # Then further developed by A Hrubak and renamed to rename.td,
7 # because he was not aware of newer versions, like file-rename(1p) (alias prename).
9 # This script is free software; you can redistribute it and/or modify it
10 # under the same terms as Perl itself.
12 # Larry(?)'s RCS header:
13 # RCSfile: rename,v Revision: 4.1 Date: 92/08/07 17:20:30
15 # $RCSfile: rename,v $$Revision: 1.5 $$Date: 1998/12/18 16:16:31 $
18 # Revision 1.5 1998/12/18 16:16:31 rmb1
19 # moved to perl/source
20 # changed man documentation to POD
22 # Revision 1.4 1997/02/27 17:19:26 rmb1
23 # corrected usage string
25 # Revision 1.3 1997/02/27 16:39:07 rmb1
28 # Revision 1.2 1997/02/27 16:15:40 rmb1
29 # *** empty log message ***
31 # Revision 1.1 1997/02/27 15:48:51 rmb1
38 Getopt
::Long
::Configure
('bundling', 'permute');
41 my ($no_act, $force, $op);
43 die "Usage: rename [-v[v]] [-n] [-f] perlexpr [filenames]\n"
45 'v|verbose+' => \
$verbose,
46 'n|no-act|dry-run|dryrun' => \
$no_act,
51 print STDERR
"reading filenames from STDIN\n" if $verbose;
56 my $header_emitted = 0;
57 my $highest_errno = 0;
61 return if $header_emitted;
63 print join "\t", "STATUS", "OLD", "NEW";
77 $status = "KEEP" if $verbose >= 2;
79 elsif (-e
$_ and !$force)
93 $status = "OK" if $verbose;
98 $status = "ERR $errno";
99 $highest_errno = $errno if $errno > $highest_errno;
107 print join "\t", $status, $was, $_;
118 rename.td - rename multiple files by a Perl expression
122 rename.td S<[ B<-v>[B<v>] ]> S<[ B<-n> ]> S<[ B<-f> ]> I<perlexpr> S<[ I<files> ]>
124 cat files.list | rename.td S<[ B<-v>[B<v>] ]> S<[ B<-n> ]> S<[ B<-f> ]> I<perlexpr>
128 B<rename.td> renames the files supplied according to the rule specified as the first argument.
129 The I<perlexpr> argument is a Perl expression which is expected to modify the C<$_>
130 string in Perl for at least some of the filenames specified.
131 If a given filename is not modified by the expression, it will not be renamed.
132 If no filenames are given on the command line, filenames will be read via standard input.
134 For example, to rename all files matching C<*.bak> to strip the extension,
137 rename.td 's/\.bak$//' *.bak
139 To translate uppercase names to lower, you'd use
141 rename.td 'y/A-Z/a-z/' *
147 =item B<-v>, B<--verbose>
149 Verbose: print names of files successfully renamed.
153 Verbose extra: print names of files of which name is not changed.
155 =item B<-n>, B<--dry-run>, B<--no-act>
157 No Action: show what files would have been renamed, or skipped.
159 =item B<-f>, B<--force>
161 Force: overwrite existing files.
167 Output Tab-delimited fields line-by-line.
168 First line is the headers.
169 Each subsequent line describes a file in this way:
173 =item 1st field - status
177 =item B<KEEP> - no change in file name, shown in B<-vv> mode
179 =item B<SKIP> - destination already exists, not in B<--force> mode
181 =item B<WOULD> - would be attempted to rename, in B<--dry-run> mode
183 =item B<OK> - successfully renamed
185 =item B<ERR I<nnn>> - error happened during rename, error code is I<nnn>
189 =item 2nd field - old file name
191 =item 3rd field - new file name
197 Zero when all rename succeeded, otherwise the highest error number of all the failed renames, if any.
198 See rename(2) for these error numbers.
202 No environment variables are used.
206 Larry Wall (author of the original)
212 mv(1), perl(1), rename(2), file-rename(1p) (prename(1)), rename.ul (rename(1)), renamemanual(1)
216 If you give an invalid Perl expression you'll get a syntax error.