#!/usr/bin/perl -w # vim: set sw=4 ts=4 si et: # sub help(); help() unless ($ARGV[0]); help() if ($ARGV[0] eq "-h"); my $was; my $status = 0; for (@ARGV) { next if (-d $_); # directory $was=$_; s/[^A-Za-z0-9\.\-]+/_/g; s/^_+//; next if ($was eq $_); # no change if (-e "$_" ){ print STDERR "ERROR: file $_ does already exist\n"; $status = 1; next; } if (rename($was, $_)) { print "$was -> $_\n"; }else{ print STDERR "$0: can't rename $was to $_: $!\n"; $status = 1; } } exit($status); sub help(){ print "Usage: rename_cleanname [-h] files Remove annoying characters such as space and shell meta characters form file names and replace them by underscore. Example: rename_cleanname * This gives unix friendly names to all files in the current directory. rename_cleanname \'some document.doc\' Generates some_document.doc \n"; exit(0); } #----------------------------------------------------------