Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename multiple file

Status
Not open for further replies.

mrimagepueblo

Programmer
Dec 15, 2003
52
US
I want to create a shell script that will automatically rename a bunch of graphics files cutting off the last part of the filename as their is no pattern to match on without a bunch of if statements. I can create the shell script just need the pattern to rename.
The filenames for example are
5568012_101_12.jpg
5652901_101_12.jpg
5890390_101_13.jpg
5992313_101_71.jpg

I simply want the files to be renamed as
5568012.jpg
5652901.jpg
5890390.jpg
5992313.jpg

thanks a bunch in advance
 
$1 will grab the first part of the filename and $2 will grab the suffix

if you need help with the renaming bit just shout

Code:
[b]#!/usr/bin/perl[/b]

while (<DATA>) {
  chomp;
  s/^([^_]+).*(\..*)$//;
  print "$1$2\n";
}

__DATA__
5568012_101_12.jpg
5652901_101_12.jpg
5890390_101_13.jpg
5992313_101_71.jpg


Kind Regards
Duncan
 
I do need help! I created a rename.sh file and nothing happens

------------
#!/bin/sh
while (<DATA>) {
chomp;
s/^([^_]+).*(\..*)$//;
print "$1$2\n";
}
exit 0
-------------
 
... this is a Perl forum

maybe you could post in the UNIX Scripting Forum


Kind Regards
Duncan
 
thanks for your effort. I will try there, and I'll also try and figure out what you were suggesting here as well.
 
no problem - i can help you with a Perl solution if you need one - i'm just a bit confused as to why you ended up here? did you intend to?


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top