#!/usr/bin/perl # $Id: mmv,v 1.8 2006/08/24 01:21:24 winston Exp $ use English; use Getopt::Std; getopts('htq'); $0 =~ s/.*\///; # clean leading path if (int(@ARGV)!=2 or $opt_h) { { print< mmv renames all files in the curent directory with the given search string and replaces the search string with the replacement string. Options: -h Print this help message. -t Do a test run; show what would be changed, but don't rename files. -q Quiet; don't print filenames as they are being renamed. If the search string starts with a '-', you will need to put a '--' between the options and the search string. Example: mmv .htm .html This would rename all files with '.htm' to '.html' END } exit 1; } $search = $ARGV[0]; $replace = $ARGV[1]; opendir DIR, './'; @files = readdir DIR; @changefiles; foreach $file (@files){ if ($file eq '.'){ next; } if ($file eq '..'){ next; } if ($file =~ /$search/){ # Create the new filename, replace all instances $newfile = $file; $newfile =~ s/$search/$replace/g; if (! $opt_q) { print "$file -> $newfile\n"; } if (! $opt_t) { $ret = rename "$file", "$newfile"; if ($ret==0) { print "Error: $file"; } } } }