From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

#!/usr/bin/perl -w
# Author : Johan Vromans
# Created On : Tue Sep 15 15:59:04 1992
# Last Modified By: Johan Vromans
# Last Modified On: Tue Dec 15 14:59:05 2020
# Update Count : 158
# Status : Unknown, Use with caution!
################ Common stuff ################
use strict;
################ Command line parameters ################
my $showonly = 0; # just show, do nothing
my $reverse = 0; # process in reverse order
my $overwrite = 0; # overwrite existing files
my $createdirs = 0; # create missing dirs
my $link = 0; # link instead of rename
my $symlink = 0; # symlink instead
my $verbose = 0; # more verbosity
my $trace = 0; # tracing
# Process command line options.
app_options();
################ Presets ################
################ The Process ################
# Get the command string;
my $cmd = shift;
pmv( $cmd, \@ARGV,
{ showonly => $showonly,
reverse => $reverse,
overwrite => $overwrite,
createdirs => $createdirs,
link => $link,
symlink => $symlink,
verbose => $verbose,
trace => $trace,
} );
exit;
################ Command Line Options ################
use Getopt::Long qw(:config bundling);
sub app_options {
eval { Getopt::Long::->VERSION(2.34) }; # will enable help/version
GetOptions(ident => \&app_ident,
'verbose|v' => \$verbose,
'trace' => \$trace,
# application specific options go here
'link|l' => \$link,
'symlink|s' => \$symlink,
'dry-run|n' => \$showonly,
'reverse|r' => \$reverse,
'overwrite|o' => \$overwrite,
'make-dirs|p' => \$createdirs,
)
or Getopt::Long::HelpMessage(2);
}
sub app_ident {
print STDOUT ("This is File::PerlMove [pmv $File::PerlMove::VERSION]\n");
}
__END__
=head1 NAME
pmv - rename files using Perl expressions
=head1 SYNOPSIS
pmv [options] expression [file ...]
Options:
--dry-run -n show, but do not do it
--link -l link instead of rename
--symlink -s symlink instead of rename
--reverse -r process in reverse order
--overwite -o overwrite existing files
--make-dirs -p create target dirs, if necessary
--verbose -v verbose information
--ident show identification
--help brief help message
=head1 DESCRIPTION
B<pmv> will apply the given Perl expression to each of the
file names. If the result is different from the original name, the
file will be renamed, linked, or symlinked.
If the expression is any of C<uc>, C<lc>, of C<ucfirst>, B<pmv> will DWIM.
Note that these are pretty useless on file systems that are case insensitive.
B<pmv> is a wrapper around File::PerlMove, which does most of the work.
=head1 OPTIONS
=over 8
=item B<--dry-run> B<-n>
Show the changes, but do not rename the files.
=item B<--link> B<-l>
Link instead of rename.
=item B<--symlink> B<-s>
Symlink instead of rename. Note that not all platforms support symlinking.
=item B<--reverse> B<-r>
Process the files in reversed order.
=item B<--overwrite> B<-o>
Overwrite existing files.
=item B<--make-dirs> B<-p>
Create target directories if necessary.
=item B<--verbose> B<-v>
More verbose information.
=item B<--version>
Print a version identification to standard output and exits.
=item B<--help>
Print a brief help message to standard output and exits.
=item B<--ident>
Prints a program identification. Processing continues.
=item I<file>
File name(s).
=back
=head1 EXAMPLES
To change editor backup files back to Perl sources:
$ pmv -v 's/\.bak$/.pl/' *.bak
foo.bak => foo.pl
bar.bak => bar.pl
Lowcase file names:
$ pmv -v lc *JPG
DSC03345.JPG => dsc03345.jpg
DSC03346.JPG => dsc03346.jpg
DSC03347.JPG => dsc03347.jpg
Shift numbered examples to a new section:
$ pmv --verbose --reverse 's/^ex(\d)/"ex".($1+3)/ge' ex*
ex42.dat => ex72.dat
ex25.dat => ex55.dat
ex22.dat => ex52.dat
ex13.dat => ex43.dat
ex12.dat => ex42.dat
Note that these need to be processed in reversed order, to prevent
C<< ex12.dat => ex42.dat >> botching with the exisitng C<ex42.dat>.
=head1 SEE ALSO
File::PerlMove, App::perlmv (and perlvm), File::Rename (and rename).
=head1 AUTHOR
Johan Vromans <jvromans@squirrel.nl>
=head1 COPYRIGHT
This programs is Copyright 2004,2010,2011,2017 Squirrel Consultancy.
This program is free software; you can redistribute it and/or modify
it under the terms of the Perl Artistic License or the GNU General
Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
=cut