LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2009, 07:31 AM   #1
punj33
LQ Newbie
 
Registered: May 2009
Posts: 3

Rep: Reputation: 0
problem with rename


my problem is
suppose i have files
punj0001.JPG
punj0002.JPG
and so on.

and i want it to be renamed as
holiday_punj0001_home.JPG
and so on
so i did:
rename -n 's/\(.\{8\}\)/holiday_\1_home/' *.JPG (keeping in mind SED)
then it said to change \1 to $1
then on
rename -n 's/\(.\{8\}\)/holiday_\1_home/' *.JPG
it didnt show any output
could you help me out ?
or is it the rules of sed doesn't apply to rename
i have no idea about PERL (is it needed ?)
 
Old 05-26-2009, 07:51 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nope. From the rename man page, the arguments from and to are patterns, not sed commands. Using rename, you can do it in two passes:
Code:
$ rename pun holiday_pun *.JPG
$ rename .JPG _home.JPG *.JPG
otherwise use a for loop:
Code:
for file in *.JPG
do
  mv $file $(echo $file | sed 's/\(.\{8\}\)/holiday_\1_home/')
done
 
Old 05-26-2009, 02:19 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
There are two versions of rename:

Binary
http://linux.die.net/man/1/rename

Perl script (Debian-based Linux distros)
http://tips.webdesign10.com/how-to-b...n-the-terminal

The perl version takes a perl expression as its first parameter.
Perl expressions use perl regular expressions which are in many ways similar to those used with 'sed -r' and 'egrep'.
Try
Code:
rename -n 's/(.{8})/holiday_$1_home/' *.JPG
 
Old 05-26-2009, 02:37 PM   #4
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
This should do it too, using only bash.


for file in *.JPG
do
mv $file holiday_${file%.*}_home.JPG
done


Dave
 
Old 06-22-2009, 12:06 AM   #5
punj33
LQ Newbie
 
Registered: May 2009
Posts: 3

Original Poster
Rep: Reputation: 0
thanks everyone.
it worked
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
samba pdc selinux problem "rename" %m.log WorldIsNotFair Linux - Server 2 07-23-2008 09:15 PM
Rename my Fedora Core 5 box - port fowarding problem fatrandy13 Linux - Networking 1 05-26-2006 01:21 PM
rename problem estratos Programming 4 03-29-2006 02:10 AM
PHP/MYSQL problem. Rename of table-name solves the problem... mosedrengen Linux - Software 3 01-06-2006 07:07 AM
"Edit and rename swaret conf" or whatever. PROBLEM! Kjetil4455 Slackware 8 04-14-2004 03:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration