rename.pl - mangle filenames using perl expressions
Append .old to a bunch of files:
$ rename.pl 's/$/.old/' *
Fix the case on all .C files to lower, doing a preview of changes only:
$ rename.pl -p 'tr/A-Z/a-z/' *.C
A file renamer that uses user-supplied perl expressions to rename
files. Optionally, the copy() or move() routines from File::Copy may
be used instead of the builtin rename() function.
A preview mode is also available, to allow testing of expressions before any changes are allowed to go to disk.
$ rename.pl [options] expression [file1 file2 .. fileN]
See OPTIONS for details on the command line switches supported.
If no files are mentioned on the command line, the script will attempt to read them from STDIN, allowing for easy interoperation with various shell utilities. In this case, fix .htm files to .html:
$ find . ! -type d -name "*.htm" | rename.pl '$_ .= "l"'
This script currently supports the following command line switches:
copy() from File::Copy.
move() from File::Copy,
which may not be available in older versions of File::Copy.
Since rename() cannot be used across filesystem boundaries, and may
not be available on all systems, move() may be a better option in some
circumstances.
Remove upper case from a bunch of files:
$ rename.pl '$_ = lc' *
Dealing with deep directories full of files to be changed can be tricky, especially if the pattern in question is renaming directories, which will throw off the rename of deeper files.
The solution is to first change all the directories, then go after the files. For example, to swap out ``bad'' characters with _ in a deep file tree:
$ find . -type d -exec rename.pl 's/[^A-Za-z0-9,.\/_-]/_/g' {} \;
$ find . -exec rename.pl 's/[^A-Za-z0-9,.\/_-]/_/g' {} \;
Newer versions of this script may be available from:
If the bug is in the latest version, send a report to the author. Patches that fix problems or add new features are welcome.
Bugs are left as an exercise to the expression writer. :)
File::Copy, perl(1)
Jeremy Mates, http://sial.org/contact/
Copyright (c) 2000-2002, Jeremy Mates. This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This is a hacked up version of Larry Wall's original rename script distributed with perl under the eg directory, improved by Daniel V. Klien (copy option, interactive mode), and then polished into this form by Jeremy Mates (preview mode, comments in source, move support).
$Id: rename.pl,v 2.4 2003/01/13 05:28:42 jmates Exp $