Fedora Linux Support Community & Resources Center
  #1  
Old 16th November 2012, 07:48 PM
Yurij Offline
Registered User
 
Join Date: Aug 2012
Location: moscow, ru
Posts: 18
Bash for batch renaming

It risk to do a duplicate but i didn't find a solution yet. I have a bunch of files img001 img002 img003 etc ... i need a batch rename it to only even or odd. Such way the 3+ files mentioned before become img001 img003 img005. I think it related to bash scripting something like for f in * do i++; newname="img"+"00"+(i+(i+1) mv oldname newname.

Gusti sono gusti
__________________
gusti sono gusti
Reply With Quote
  #2  
Old 16th November 2012, 08:25 PM
markus39 Online
Registered User
 
Join Date: Jul 2008
Location: Maastricht, the Netherlands
Age: 46
Posts: 65
linuxfirefox
Re: Bash for batch renaming

#!/bin/bash

mkdir ./NEWFILES
COUNT=1
ls img* | while read FILE
do
cp "$FILE" ./NEWFILES/img$(printf "%04d" $COUNT)
COUNT=$((COUNT + 2))
done

Last edited by markus39; 16th November 2012 at 08:44 PM.
Reply With Quote
  #3  
Old 16th November 2012, 08:39 PM
milheim05's Avatar
milheim05 Offline
Registered User
 
Join Date: Jul 2008
Posts: 35
windows_7firefox
Re: Bash for batch renaming

markus, could you provide an output of that working? I'm not sure it'll do what Yurij is looking for. The problem I foresee is if you have img001, img002, img003, img004, and img005, and you want to rename so they're only odd, then img002 would overwrite img003, and img004 would overwrite img005, etc...
Reply With Quote
  #4  
Old 16th November 2012, 08:48 PM
markus39 Online
Registered User
 
Join Date: Jul 2008
Location: Maastricht, the Netherlands
Age: 46
Posts: 65
linuxfirefox
Re: Bash for batch renaming

Sorry, didn't read exactly what he wanted, created more a proof of concept.
But now I edited the reply to be exactly what he wanted.

Edit: when experimenting mv is never a good idea! Use cp to a new directory map.

Bash is more fun when you google things and build it yourself.
I can advise http://tldp.org/LDP/abs/html/ as reference.

Learn pipelining, the commands find, grep, sed, awk and the world lies on your feet

Last edited by markus39; 16th November 2012 at 08:55 PM.
Reply With Quote
  #5  
Old 16th November 2012, 08:54 PM
milheim05's Avatar
milheim05 Offline
Registered User
 
Join Date: Jul 2008
Posts: 35
windows_7firefox
Re: Bash for batch renaming

Quote:
Originally Posted by markus39 View Post
Sorry, didn't read exactly what he wanted, created more a proof of concept.
But now I edited the reply to be exactly what he wanted.

Bash is more fun when you google things and build it yourself.
I can advise http://tldp.org/LDP/abs/html/ as reference.

Learn pipelining, the commands find, grep, sed, awk and the world lies on your feet
Very nice! A word to the OP, this will depend on how many files you have. The %.04d means that your files will be renamed in a new directory as img0001, img0003...img0011. This is a problem if you already have a width of 4 numbers on your img files. In which case just adjust the width to as long as you need.
Reply With Quote
  #6  
Old 16th November 2012, 08:54 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,105
linuxfirefox
Re: Bash for batch renaming

Ummm.

No. Your COUNT goes 1,2,3... and the need was for 1,3,5. So at a minimum you need $((COUNT + 2)

But even then, it doesn't necessarily work. Find will identify files, but only in the order they were created. This means that if the file img001 were created before img002 before img003... then things should work. But if they were modified/updated (as in deleted and new file given the same name) then they will be out of order. To get around that requires extracting the existing number and compute the new value.

Now in EITHER case, it still won't work - Think about what happens to the original "img003" when renaming. Note img002 becomes img003 (oops - original img003 gets destroyed)...

That requires the mv to MOVE the original image to a different directory...

The mapping function would then have to do a 1->1, 2->3, 3->5, 4->7, 5->9, 6->11,7->13...

Leading to a formula like n=(2*v-1) where n is the new number assigned to the file, and v is the original number associated with the file.

This should preserve the ordering of the images, but will/can leave holes if something like img005 is deleted, then having the renaming done means that img009 will not exist (2*5-1).

If you wanted to remove these skips, then using the formula method will not work either - you need to generate the names (possibly using find), but then sort them to get the order right. Now you have a list of names and the original renumbering (the count=count+2 method) would work correctly.
Reply With Quote
  #7  
Old 16th November 2012, 09:01 PM
markus39 Online
Registered User
 
Join Date: Jul 2008
Location: Maastricht, the Netherlands
Age: 46
Posts: 65
linuxfirefox
Re: Bash for batch renaming

Already fix it....

---------- Post added at 10:01 PM ---------- Previous post was at 09:57 PM ----------

NUMBEROFFILES=$(($(ls img* | wc -l) * 2)); echo ${#NUMBEROFFILES} zeroes

Last edited by markus39; 16th November 2012 at 09:28 PM.
Reply With Quote
  #8  
Old 23rd November 2012, 03:11 AM
ocratato Offline
Registered User
 
Join Date: Oct 2010
Location: Canberra
Posts: 547
linuxfirefox
Re: Bash for batch renaming

Rather than moving or copying the files, I used ln to link them when I was doing something similar for a batch of video files.

I would also recommend building the new structure in a separate directory so that there is much less chance of clobbering things and you can just delete the whole shebang and start over if it doesn't quite come out as you expect.
Reply With Quote
Reply

Tags
bash, batch, renaming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script, ffmpeg batch help needed AndrewSerk Using Fedora 3 18th December 2011 12:02 AM
Batch Renaming kaizoku001 Using Fedora 3 28th August 2010 09:30 AM
Batch Converting Files with BASH Starclopsofish Using Fedora 1 10th February 2009 12:00 PM


Current GMT-time: 23:52 (Wednesday, 22-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat