#!/usr/bin/perl # rename_without_endspace # This script renames the files supplied as command-line args # to remove leading or trailing whitespace in the filenames. # (Whitespace = spaces, tabs, carriage returns) # E.g. a file " foobar" is renamed to "foobar" # and a file "barfoo " is renamed to "barfoo" # Note that since space characters are the usual command-line arg separators, # you will need to take care to quote the filenames or to otherwise sidestep # this problem. One way of doing this would be to use the '-print0' option # to 'find' along with the '-0' option to xargs - for example: # /usr/bin/find StartDir -print0 | xargs -0 rename_without_endspace # # Cameron Hayne (macdev@hayne.net) Oct 2005 use strict; use warnings; chomp(@ARGV = ) unless @ARGV; foreach my $filename (@ARGV) { my $orig_filename = $filename; $filename =~ s/^\s+//; # strip leading whitespace $filename =~ s/\s+$//; # strip trailing whitespace unless ($filename eq $orig_filename) { print "About to rename \"$orig_filename\" to \"$filename\"\n"; if (-e $filename) { print "Oops, there already exists a file named \"$filename\"\n"; print "Skipping the rename - you will have to do it manually\n"; } else { rename($orig_filename, $filename); } } }