LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 05-02-2004, 06:44 PM   #1
hellblade
Member
 
Registered: Jan 2004
Location: Greece
Distribution: Debian Sid
Posts: 58

Rep: Reputation: 15
Batch rename question


Hi.

Is there any way to batch rename some files.
To be more specific my files have the following name format:

1111.aaaa.bbbb.2222.3333.jpg

1111, 2222, 3333 -> remane the same to all files
aaaa, bbbb -> differs from file to file


I want them to become:

1111 - aaaa - bbbb.jpg

I hope it's clear what I want to do.

Thanks in advance.

Last edited by hellblade; 05-02-2004 at 06:46 PM.
 
Old 05-02-2004, 07:05 PM   #2
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
Do you actually want spaces in the filenames? I'm not good in dealing with spaces, so if you don't want 'em this will work:

Code:
for i in *.jpg; do mv $i `echo $i | awk -F'.' '{print $1"-"$2"-"$3".jpg"}'`; done
Backup your files before trying it, in case I'm wrong haha (didn't thoroughly test it, but it seems right).

Last edited by rkef; 05-02-2004 at 07:31 PM.
 
Old 05-03-2004, 05:52 AM   #3
hellblade
Member
 
Registered: Jan 2004
Location: Greece
Distribution: Debian Sid
Posts: 58

Original Poster
Rep: Reputation: 15
Thanks for your reply.
I don't understand this command so I can't tweak it for the spaces. If I try:
Code:
for i in *.jpg; do mv $i `echo $i | awk -F'.' '{print $1" - "$2" - "$3".jpg"}'`; done
it sais that the last argument has to be a directory so if somebody could help I would appretiate it.

And something else. In my example bbbb is in_this_format. Can I make it in_that_way?

Last edited by hellblade; 05-03-2004 at 05:54 AM.
 
Old 05-03-2004, 12:23 PM   #4
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
(edit: it just needed quotes around the second argument to mv . I'm slow!)

Spaces in filename are generally highly annoying, but if it's what you really want this will work:
Code:
for i in *.jpg; do mv $i "`echo $i | awk -F'.' '{print $1" - "$2" - "$3".jpg"}'`"; done
The reason the command works, step by step:

Code:
1) 'for i in *.jpg; do' - this construct loops over ever file ending
    in .jpg, in the current dir
2) 'mv $i' - mv each file, in turn, to the result of the evaluation of 3)
3) "`echo $i | awk -F'.' '{print $1" - "$2" - "$3".jpg"}'`" - from left to right:
        i)  echo the filename
        ii) pipe the filename into awk, which splits it into strings, based
            on the field separator (-F'.' sets the field separator to the
            period).  String names are $1, $2, $3, ..., $6 in this case
        iii) the last part of the awk expression prints fields 1, 2, 3
            with hyphens/spaces in between, follow by .jpg;
             this print statement is the final value returned as the
             second argument of the mv command in 2)
Hope that was useful.

Last edited by rkef; 05-03-2004 at 02:51 PM.
 
Old 05-03-2004, 03:57 PM   #5
dARkHunTEr
Member
 
Registered: Jul 2003
Location: The Netherlands
Distribution: Gentoo Linux
Posts: 213

Rep: Reputation: 30
You might want to try krename if you're using X.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch Script to rename files... jamie_barrow Linux - Newbie 16 06-14-2009 01:26 PM
batch vs ?????? munna_502 Linux - Software 6 04-01-2004 09:42 PM
Batch Question hornet74 Linux - General 2 02-02-2004 04:39 PM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 06:52 AM
Linux Backup Batch question lowly_tech Linux - Newbie 2 01-09-2002 04:02 PM


All times are GMT -5. The time now is 12:41 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration