 |
 |
 |
 |
| Programming & Packaging A place to discuss programming and packaging. |

16th November 2012, 07:48 PM
|
|
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
|

16th November 2012, 08:25 PM
|
|
Registered User
|
|
Join Date: Jul 2008
Location: Maastricht, the Netherlands
Age: 46
Posts: 65

|
|
|
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.
|

16th November 2012, 08:39 PM
|
 |
Registered User
|
|
Join Date: Jul 2008
Posts: 35

|
|
|
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...
|

16th November 2012, 08:48 PM
|
|
Registered User
|
|
Join Date: Jul 2008
Location: Maastricht, the Netherlands
Age: 46
Posts: 65

|
|
|
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.
|

16th November 2012, 08:54 PM
|
 |
Registered User
|
|
Join Date: Jul 2008
Posts: 35

|
|
|
Re: Bash for batch renaming
Quote:
Originally Posted by markus39
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.
|

16th November 2012, 08:54 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,105

|
|
|
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.
|

16th November 2012, 09:01 PM
|
|
Registered User
|
|
Join Date: Jul 2008
Location: Maastricht, the Netherlands
Age: 46
Posts: 65

|
|
|
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.
|

23rd November 2012, 03:11 AM
|
|
Registered User
|
|
Join Date: Oct 2010
Location: Canberra
Posts: 547

|
|
|
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.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 23:52 (Wednesday, 22-05-2013)
|
|
 |
 |
 |
 |
|
|