On Sat, 11 Jan 2003 13:42:48 +0000 Anne Wilson <[EMAIL PROTECTED]> wrote:
> On Saturday 11 Jan 2003 4:46 am, Todd Slater wrote: > > On Fri, 10 Jan 2003 20:31:05 -0800 > > > > Matt Florido <[EMAIL PROTECTED]> wrote: > > > I was wondering if someone could assist me in creating a shell > > > script that will take the contents of a directory and rename them a > > > certain way. > > > > > > For example - 4 files in a directory: > > > a.jpg > > > ab.jpg > > > abc.jpg > > > abcd.jpg > > > > > > Renamed as: > > > 1-pic.jpg > > > 2-pic.jpg > > > 3-pic.jpg > > > 4-pic.jpg > > > > > > So it takes the contents of the directory regardless of the current > > > filename, and renames them in an incrementing format. > > > > > > ((N+1) + "-something.jpg") > > > > Change the variables to your taste. The printf in the script will > > write a leading 0 in the number--that's always a gotcha when the order > > is important. If you have more than 99 images, you can change the 2 to > > a 3 and you'll get 001, 002 etc. > > > > Test it out first, then change "echo" to "mv", remove the quotes > > around the expression after that, take out the ==> of course. > > HTH, > > Todd > > > > #!/bin/bash > > EXT=".jpg" > > NAME=pic > > CNT=1 > > for image in `ls /path/to/directory | sort` > > do > > FCNT=`printf "%02d" $CNT` > > echo "$image ==> $FCNT-$NAME$EXT" > > CNT=$(($CNT+1)) > > done > > exit > > Just wondering - sorting numbered files is always problematic. Could > you pad to 3 digits, sort of > > if length-of numberstring < 3, the for 3-length-of-numberstring print 0 > > then continue as before? > > I haven't yet learned syntax for bash programming, but I guess you will > understand what I mean. > > Anne If I understand your question correctly, that's what this line does: FCNT=`printf "%02d" $CNT` (That forces it to be 2 digits, with a leading 0 if necessary. Changing the 2 to a 3 will force 3 digits with leading 0's.) $CNT is the number variable that gets incremented by 1 for each file the script processes. $FCNT formats that number to be 2 digits. Todd
Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
