↑↑↑ Home | ↑↑ UNIX | ↑ Updateware |
Perlers will know rename, the automatic file renamer originally written by Larry Wall, which comes with the Perl interpreter. Though very powerful, it cannot rename files as successive numbers, which is something one often wants. This cannot be fixed by a crafty Perl expression, either, since the expression is within the scope of one cycle of the loop.
For that reason I added an index variable $i to rename which is incremented for each processed file. By using that variable in the Perl expression, one can enumerate files or more generally rename them based on their position on the command line (or stdin). Because it is most practical in the most frequent case of enumerating files, the index variable starts at 1 and is padded with leading zeros to have the same width as the maximum index. When the index is used in arithmetic, Perl discards the leading zeros.
Get the modified rename: Download
Make it executable, put it somewhere in your path, extract the manual page (also updated) with pod2man or pod2html, and enjoy!
rename - renames multiple files
rename [-bfilnv] [-B prefix] [-S suffix] [-V method] [-Y prefix] [-z suffix] [--backup] [--basename-prefix=prefix] [--dry-run] [--force] [--help] [--interactive] [--just-print] [--link-only] [--prefix=prefix] [--suffix=suffix] [--verbose] [--version-control=method] [--version] perlexpr [files]...
rename renames the filenames supplied according to the rule specified as the first argument. The argument is a Perl expression which is expected to modify the $_ string for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input.
If a destination file is unwritable, the standard input is a tty, and the -f or --force option is not given, mv prompts the user for whether to overwrite the file. If the response does not begin with `y' or `Y', the file is skipped.
Make backup files. That is, when about to overwrite a file, rename the original instead of removing it. See the -V or --version-control option fo details about how backup file names are determined.
Use the simple method to determine backup file names (see the -V method or --version-control=method option), and prepend prefix to a file name when generating its backup file name.
Remove existing destination files and never prompt the user.
Print a summary of options and exit.
Prompt whether to overwrite each destination file that already exists. If the response does not begin with `y' or `Y', the file is skipped.
Link files to the new names instead of renaming them. This will keep the original files.
Do everything but the actual renaming, insted just print the name of each file that would be renamed. When used together with --verbose, also print names of backups (which may or may not be correct depending on previous renaming).
Print the name of each file before renaming it.
Use method to determine backup file names. The method can also be given by the RENAME_VERSION_CONTROL (or if that's not set, the VERSION_CONTROL) environment variable, which is overridden by this option. This option does not affect wheter backup files are made; it affects only the name of any backup files that are made.
The value of method is like the GNU Emacs `version-control' variable; rename also recognize synonyms that are more descriptive. The valid values are (unique abbreviations are accepted):
Make numbered backups of files that already have them, otherwise simple backups. This is the default.
Make numbered backups. The numbered backup file name for F is F.~N~ where N is the version number.
Make simple backups. The -B or --prefix, -Y or --basename-prefix, and -z or --suffix options specify the simple backup file name. If none of these options are given, then a simple backup suffix is used, either the value of SIMPLE_BACKUP_SUFFIX environment variable if set, or ~ otherwise.
Print version information on standard output then exit successfully.
Use the simple method to determine backup file names (see the -V method or --version-control=method option), and prefix prefix to the basename of a file name when generating its backup file name. For example, with -Y .del/ the simple backup file name for a/b/foo is a/b/.del/foo.
Use the simple method to determine backup file names (see the -V method or --version-control=method option), and append suffix to a file name when generating its backup file name.
To rename all files matching *.bak to strip the extension, you might say
rename 's/\e.bak$//' *.bak
To translate uppercase names to lower, you'd use
rename 'y/A-Z/a-z/' *
The index of the file to be renamed is made available in the 1-based $i
variable, so you can enumerate files in chronological order like this:
ls -rt1 | rename 's/^/${i}_/'
More examples:
rename 's/\.flip$/.flop/' # rename *.flip to *.flop
rename s/flip/flop/ # rename *flip* to *flop*
rename 's/^s\.(.*)/$1.X/' # switch sccs filenames around
rename 's/$/.orig/ */*.[ch]' # add .orig to source files in */
rename 'y/A-Z/a-z/' # lowercase all filenames in .
rename 'y/A-Z/a-z/ if -B' # same, but just binaries!
or even
rename chop *~ # restore all ~ backup files
Two environment variables are used, SIMPLE_BACKUP_SUFFIX and VERSION_CONTROL. See "OPTIONS".
mv(1) and perl(1)
If you give an invalid Perl expression you'll get a syntax error.
Peder Stray <pederst@cpan.org>, original script from Larry Wall.