#!/usr/bin/perl # ^^^^^^^^^^^^^ change this to suit your system # if you don't know what it should be, # type this at a prompt: which perl # ** or ** # if perl is in your path somewhere you can erase # all of the above, and uncomment the 2 lines below: eval 'exec perl -S $0 "$@"' if 0; # # don't modify below here #------------------------- $ver = '1.2'; # chcase 1.2 # Changes case of filenames # http://www.blemished.net/chcase.html # supermike@blemished.net # # 1.2 - June 22, 2000 # Added -n since escape characters don't work on all terminals # Added -l to rename & follow symbolic links # Allowed options to be put together eg: chcase -rdou ... # 1.1 - Feb 29, 2000 # Added ability to supply perl expressions # 1.0 - Jan 26, 2000 # Initial Release sub ERR { print "\nchcase $ver\n"; if (shift) { print< chcase My.DOC *.JPG FileName.txt => these specific files are changed to lower case > chcase -rd '*' => all files and sub dirs to lower case, start in current dir, recurse > chcase -s pics '*.jpg' '*.gif' => rename .jpg and .gif files in pics dir to lower case > chcase -rous /tmp/junk 'a??b*.txt' => starting from /tmp/junk and recursing, change files matching this mask to all upper case, overwrite if file exists > chcase -x 's/99dec/jan2000/' -x 's/ /_/g' '*.doc' => renames *.doc files replacing 99dec with jan2000, and replacing all spaces with underscores > chcase -x 'tr/a-zA-Z0-9.//cd' -x 's/jpg/jpg/i' '*' => removes all non-alphanumeric characters except for '.' and changes the pattern "jpg" to lower case if it isn't already > chcase -r => displays directory structure EOT } else { print<] [-x ''] '' -e : Print EXAMPLES - very helpful! -r : Rename recursively -d : Also rename subdirectories -o : Overwrite if file exists -u : Change to upper case (default is lower) -q : Quiet mode (no output) -n : No escape characters (for bold output) -l : Rename & follow symbolic links (default is not to) -s : Specify starting directory -x '' : Perl expression to operate on filename usually s/// or tr/// (yes you need the quotes) case of filename not changed when this option used you can supply multiple expressions '' : Mask to rename (quotes are nice to have here) not case sensitive, you can use multiple masks -or- just supply the filename(s) on the command line EOT } exit; } sub chcase { $_ = $old = shift; foreach $m (@mask) { if (/^$m$/i) { if (@exp) { foreach $x (@exp) { eval $x; } } else { $_ = $upper ? uc : lc; } return($_) if (-d); return($_) if (!$overw && -f || ($old eq $_)); $error = (rename($old, $_)) ? '' : "$bold -> cannot rename$unbold"; if (-d) { print @h, "$bold$old/ => $_$unbold\n" unless $quiet; } else { print @h, "$old => $_$error\n" unless $quiet; } } } return($_); } sub procdir { $_ = shift; chdir($_) || die "Couldn't chdir into $_\n$!"; print @h, "$bold$_$unbold\n" unless $quiet; push @h, '|__ '; opendir(D,'.'); foreach (readdir(D)) { next if (($_ eq '.') || ($_ eq '..')); next if (-l && !$links); if (-d) { if ($rendir) { $_ = &chcase($_); next if $error; } if ($recur) { &procdir($_); chdir('..'); pop @h; } } else { &chcase($_); } } closedir(D); } $bold = "\033[01m"; $unbold = "\033[0m"; $dir = '.'; &ERR unless @ARGV; while (@ARGV) { $_ = shift; if (/^-/) { &ERR unless /^-[erdouqnlsx]+$/; /e/ && &ERR(1); /r/ && $recur++; /d/ && $rendir++; /o/ && $overw++; /u/ && $upper++; /q/ && $quiet++; /n/ && ($bold=$unbold=''); /l/ && $links++; /s/ && ($dir = shift) && ($dir =~ s/\/$//); /x/ && (push @exp, shift); } else { if (/(.*)\/([^\/]*)$/) { $dir = $1; $_ = $2; } s/\\/\\\\/g; s/\./\\./g; s/\+/\\+/g; s/\[/\\[/g; s/\]/\\]/g; s/\|/\\|/g; s/\^/\\^/g; s/\*/\.\*/g; s/\?/\./g; push @mask, $_; } } &procdir($dir);