Reputation: 65
I want to rename part of the file using perl . I know how to rename the filenames having spaces , I made this for the files names has "my 123.log". This code will rename it to new_123.log.
foreach my $i (<"my *.log">) {
my $y = $i; # save the original file name
$i =~ s/my /new_/; # replace the spaces with a _ character
rename ( $y, $i ); # rename the original file with the new name
Now I want to rename the file named as my.123.log, and I am confused how to deal with this '.' in the file name . also If I want to rename a single file name that start with the name of my (lest say my 123.log) so is this command OK ? I want to use this because I just know the starting of filename
move '../my *.log', '../new.log' or die "cant change the name";
Upvotes: 0
Views: 482
Reputation: 732
I think you would want to find the pattern before handing it off to move
. The move
statement as you have above will overwrite all input files except for the last. So, you can "save" what matched by using parens(one or more times):
/^my\s([a-z0-9])\.log/;
#try this first please
print "move '../my $1.log', '../new$1.log' or die "cant _overwrite_ the file";
#and if that looks ok then try uncommenting this:
#move '../my $1.log', '../new$1.log' or die "cant _overwrite_ the file";
I didn't have time to test my regular expression, the character class matches might need to be modified to be once or more, or extended if you think characters other than alpha numeric will be in the filenames.
Upvotes: 1